problem
solution1:
class Solution { public: bool backspaceCompare(string S, string T) { return backspace(S)==backspace(T); } string backspace(string str) { string res =""; for(auto ch:str) { if(ch=='#') { if(!res.empty()) res.pop_back(); } else res.push_back(ch); } return res; } };
solution2:
class Solution { public: bool backspaceCompare(string S, string T) { string s = "", t = ""; for(auto ch:S) ch=='#' ? (s.empty()? void():s.pop_back()) : s.push_back(ch);//err... for(auto ch:T) ch=='#' ? (t.empty()? void():t.pop_back()) : t.push_back(ch); return s==t; } };
参考
1. Leetcode_easy_844. Backspace String Compare;
2. grandyang;
完