sacgajcvs

Untitled

Apr 10th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. class Solution {
  2. public:
  3. bool backspaceCompare(string s, string t) {
  4. int i=s.length()-1,cnt1=0;
  5. int j=t.length()-1,cnt2=0;
  6. while(i>=0 && j>=0)
  7. {
  8. if(s[i]=='#'){
  9. cnt1++;
  10. i--;
  11. continue;
  12. }
  13. if(t[j]=='#'){
  14. cnt2++;
  15. j--;
  16. continue;
  17. }
  18. if(cnt1)
  19. {
  20. cnt1--;
  21. i--;
  22. continue;
  23. }
  24. if(cnt2)
  25. {
  26. cnt2--;
  27. j--;
  28. continue;
  29. }
  30. if(s[i]!=t[j])
  31. return 0;
  32. i--;
  33. j--;
  34. }
  35. while(i>=0){
  36. if(s[i]=='#'){
  37. cnt1++;
  38. }
  39. else if(cnt1)
  40. cnt1--;
  41. else
  42. return 0;
  43. i--;
  44. }
  45. while(j>=0){
  46. if(t[j]=='#'){
  47. cnt2++;
  48. }
  49. else if(cnt2)
  50. cnt2--;
  51. else
  52. return 0;
  53. j--;
  54. }
  55. return 1;
  56. }
  57. };
Advertisement
Add Comment
Please, Sign In to add comment