Advertisement
nikunjsoni

844

Jun 27th, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool backspaceCompare(string S, string T) {
  4.         int i = S.size() - 1, j = T.size() - 1, countA = 0, countB = 0;
  5.         while(i >= 0 || j >= 0){
  6.             while(i >= 0 && (S[i] == '#' || countA > 0)) S[i--] == '#' ? ++countA : --countA;
  7.             while(j >= 0 && (T[j] == '#' || countB > 0)) T[j--] == '#' ? ++countB : --countB;
  8.             if(i < 0 || j < 0) return i == j;
  9.             if(S[i--] != T[j--]) return false;
  10.         }
  11.         return i == j;
  12.     }
  13. };
  14.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement