Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean backspaceCompare(String S, String T) {
- int i=S.length()-1;
- int j=T.length()-1;
- while(i>=0 || j>=0)
- {
- i=backSlashProcess(S,i);
- j=backSlashProcess(T,j);
- //done with comparing
- if(i==-1 && j==-1) return true;
- else if (i==-1 || j==-1) return false;
- else if(S.charAt(i)!=T.charAt(j)) return false;
- i--;
- j--;
- }
- return true;
- }
- //process one cycle of a backslash
- public static int backSlashProcess(String input, int index)
- {
- int hashCount=0;
- int i=index;
- while(i>=0)
- {
- if(input.charAt(i)=='#') hashCount++;
- else
- {
- if(hashCount==0) break;
- else hashCount--;
- }
- i--;
- }
- return i;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement