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)


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