Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1.     /* Get next valid char from >= index idx. Return -1 if none or out of bounds */
  2.     void get_next(string& s, int& idx){
  3.         START:
  4.         if ((idx >= s.size()) || (idx < 0)){
  5.             idx = -1; return;
  6.         }
  7.        
  8.         int b_count = 0;
  9.         while((idx >= 0) && (s[idx] == '#')){
  10.             b_count++; idx--;
  11.         }
  12.        
  13.         while((idx >= 0) && (b_count > 0)){
  14.             b_count += (s[idx--] == '#') ? 1 : -1;
  15.         }
  16.        
  17.         /* Might have another set of #, so run again */
  18.         if ((idx >= 0) && (s[idx] == '#')){
  19.             goto START;
  20.         }
  21.     }
  22.    
  23.     bool backspaceCompare(string S, string T) {
  24.         int s_ptr = S.size()-1;
  25.         int t_ptr = T.size()-1;
  26.        
  27.         while (1){
  28.             get_next(S, s_ptr); get_next(T, t_ptr);
  29.            
  30.             if ((s_ptr < 0) && (t_ptr < 0))
  31.                 return true;
  32.             if ((s_ptr < 0) || (t_ptr < 0))
  33.                 return false;
  34.             if (S[s_ptr--] != T[t_ptr--])
  35.                 return false;
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement