// 快慢指针 classSolution { public: boolbackspaceCompare(string s, string t){ // 快慢指针 int s_slow = 0; for (int fast = 0; fast < s.size(); ++fast) { // not '#' if (s[fast] != '#') s[s_slow++] = s[fast]; // is '#' else { if (s_slow > 0) { s_slow--; } } } // 新 s 长度为 s_slow
int t_slow = 0; for (int fast = 0; fast < t.size(); ++fast) { // not '#' if (t[fast] != '#') t[t_slow++] = t[fast]; // is '#' else { if (t_slow > 0) { t_slow--; } } } // 新 t 长度为 t_slow
// length is not equal if (s_slow != t_slow) returnfalse; // length is equal else { for (int i = 0; i < s_slow; i++) { // 存在一个不一样 if (s[i] != t[i]) returnfalse; } returntrue; } } };