[LeetCode] Backspace String Compare

844. Backspace String Compare

Given two strings s and t, return true if they are equal when both are typed into empty text editors. ‘#’ means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
string helper(string s) {
string S = "";
for(auto ch : s) {
if(ch == '#') {
if(S.length() > 0)
S.pop_back();
} else S.push_back(ch);
}
return S;
}
public:
bool backspaceCompare(string s, string t) {
return helper(s) == helper(t);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/26/PS/LeetCode/backspace-string-compare/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.