1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
class Solution { public: string dealwithString(string Str) { char back = '#'; int slowIndex = 0, fastIndex = 0; for(; fastIndex < Str.length(); fastIndex++){ if(Str[fastIndex] != back){ if(fastIndex != slowIndex) Str[slowIndex] = Str[fastIndex]; slowIndex++; } else if(slowIndex > 0) slowIndex--; } return Str.substr(0, slowIndex); } bool backspaceCompare(string s, string t) { return dealwithString(s) == dealwithString(t); } };
class Solution { public: string dealwithString(string Str) { char back = '#'; string result; stack<char> StrS; for(int i = 0; i < Str.length(); i++) { if(Str[i] == back) { if(!StrS.empty()) StrS.pop(); } else StrS.push(Str[i]); } while(!StrS.empty()) { result += StrS.top(); StrS.pop(); } return result; } bool backspaceCompare(string s, string t) { return dealwithString(s) == dealwithString(t); } };
|