leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0844.cpp (898B)


0 class Solution { 1 public: 2 bool backspaceCompare(string s, string t) { 3 int i = s.size() - 1, j = t.size() - 1; 4 int skipS = 0, skipT = 0; 5 6 while (i >= 0 || j >= 0) { 7 while (i >= 0) { 8 if (s[i] == '#') { 9 skipS++, i--; 10 } else if (skipS > 0) { 11 skipS--; 12 i--; 13 } else 14 break; 15 } 16 while (j >= 0) { 17 if (t[j] == '#') { 18 skipT++, j--; 19 } else if (skipT > 0) { 20 skipT--; 21 j--; 22 } else 23 break; 24 } 25 if (i >= 0 && j >= 0 && s[i] != t[j]) return false; 26 27 if ((i >= 0) != (j >= 0)) return false; 28 29 i--; 30 j--; 31 } 32 33 return true; 34 } 35 };