Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Get next valid char from >= index idx. Return -1 if none or out of bounds */
- void get_next(string& s, int& idx){
- START:
- if ((idx >= s.size()) || (idx < 0)){
- idx = -1; return;
- }
- int b_count = 0;
- while((idx >= 0) && (s[idx] == '#')){
- b_count++; idx--;
- }
- while((idx >= 0) && (b_count > 0)){
- b_count += (s[idx--] == '#') ? 1 : -1;
- }
- /* Might have another set of #, so run again */
- if ((idx >= 0) && (s[idx] == '#')){
- goto START;
- }
- }
- bool backspaceCompare(string S, string T) {
- int s_ptr = S.size()-1;
- int t_ptr = T.size()-1;
- while (1){
- get_next(S, s_ptr); get_next(T, t_ptr);
- if ((s_ptr < 0) && (t_ptr < 0))
- return true;
- if ((s_ptr < 0) || (t_ptr < 0))
- return false;
- if (S[s_ptr--] != T[t_ptr--])
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement