문제 출처:https://leetcode.com/problems/backspace-string-compare/
Backspace String Compare - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com

'#'가 나오면 자신을 포함한 앞에 하나까지 제거해줍니다.
코드는 다음과 같습니다:
class Solution {
public:
bool backspaceCompare(string s, string t) {
remove_string(s) ; remove_string(t) ;
return (s.compare(t) == 0) ? true : false ;
}
void remove_string(string & s){
for(int i = 0 ; i < s.length() ; ){
if(s[0] == '#'){
s.erase(0, 1) ;
}
else if(s[i] == '#'){
s.erase(i-1, 2) ;
i -= 1 ;
}
else
i += 1 ;
}
}
};

해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/LeetCode/P844_Backspace_String_Compare.cpp
GitHub - gurcks8989/CodingTest: CodingTest_study_with_c++
CodingTest_study_with_c++. Contribute to gurcks8989/CodingTest development by creating an account on GitHub.
github.com
훈수 및 조언은 언제든 환영입니다.
문제 출처:https://leetcode.com/problems/backspace-string-compare/
Backspace String Compare - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com

'#'가 나오면 자신을 포함한 앞에 하나까지 제거해줍니다.
코드는 다음과 같습니다:
class Solution {
public:
bool backspaceCompare(string s, string t) {
remove_string(s) ; remove_string(t) ;
return (s.compare(t) == 0) ? true : false ;
}
void remove_string(string & s){
for(int i = 0 ; i < s.length() ; ){
if(s[0] == '#'){
s.erase(0, 1) ;
}
else if(s[i] == '#'){
s.erase(i-1, 2) ;
i -= 1 ;
}
else
i += 1 ;
}
}
};

해당 문제는 Github에서도 보실 수 있습니다:
https://github.com/gurcks8989/CodingTest/blob/master/LeetCode/P844_Backspace_String_Compare.cpp
GitHub - gurcks8989/CodingTest: CodingTest_study_with_c++
CodingTest_study_with_c++. Contribute to gurcks8989/CodingTest development by creating an account on GitHub.
github.com
훈수 및 조언은 언제든 환영입니다.