给定 s
和 t
两个字符串,当它们分别被输入到空白的文本编辑器后,如果两者相等,返回 true
。#
代表退格字符。
注意:如果对空文本输入退格字符,文本继续为空。
示例 1:
输入:s = "ab#c", t = "ad#c"
输出:true
解释:s 和 t 都会变成 "ac"。
示例 2:
输入:s = "ab##", t = "c#d#"
输出:true
解释:s 和 t 都会变成 ""。
示例 3:
输入:s = "a#c", t = "b"
输出:false
解释:s 会变成 "c",但 t 仍然是 "b"。
提示:
1 <= s.length, t.length <= 200
s
和t
只含有小写字母以及字符'#'
代码
class Solution {
public:
bool backspaceCompare(string s, string t) {
string s1 = remove_ch(s);
string t1 = remove_ch(t);
int a = s1.compare(t1);
if (a == 0) {
return true;
}
else {
return false;
}
}
string remove_ch(string str) {
string result;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '#') {
if (!result.empty()) {
result.pop_back();
}
}
else {
result.push_back(str[i]);
}
}
return result;
}
};