Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 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. i=backSlashProcess(S,i);
  10. j=backSlashProcess(T,j);
  11.  
  12. //done with comparing
  13. if(i==-1 && j==-1) return true;
  14. else if (i==-1 || j==-1) return false;
  15. else if(S.charAt(i)!=T.charAt(j)) return false;
  16.  
  17. i--;
  18. j--;
  19.  
  20.  
  21. }
  22. return true;
  23. }
  24.  
  25.  
  26.  
  27.  
  28. //process one cycle of a backslash
  29. public static int backSlashProcess(String input, int index)
  30. {
  31. int hashCount=0;
  32. int i=index;
  33. while(i>=0)
  34. {
  35.  
  36. if(input.charAt(i)=='#') hashCount++;
  37. else
  38. {
  39. if(hashCount==0) break;
  40. else hashCount--;
  41.  
  42. }
  43. i--;
  44. }
  45.  
  46. return i;
  47.  
  48. }
  49.  
  50.  
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement