Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. class Solution {
  2. public boolean backspaceCompare(String S, String T) {
  3.  
  4. int i=S.length()-1;
  5. int j=T.length()-1;
  6.  
  7. while(i>=0 || j>=0)
  8. {
  9. //get the indexes once the set of backslash is done
  10. i=backSlashProcess(S,i);
  11. j=backSlashProcess(T,j);
  12.  
  13.  
  14. if(i==-1 && j==-1) return true;
  15. else if (i==-1 || j==-1) return false;
  16. else if(S.charAt(i)!=T.charAt(j)) return false;
  17.  
  18. i--;
  19. j--;
  20.  
  21.  
  22. }
  23. return true;
  24. }
  25.  
  26.  
  27.  
  28.  
  29. //process one cycle of a backslash
  30. public static int backSlashProcess(String input, int index)
  31. {
  32. int hashCount=0;
  33. int i=index;
  34. while(i>=0)
  35. {
  36.  
  37. if(input.charAt(i)=='#') hashCount++;
  38. else
  39. {
  40. if(hashCount==0) break;
  41. else hashCount--;
  42.  
  43. }
  44. i--;
  45. }
  46.  
  47. return i;
  48.  
  49. }
  50.  
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement