sunfresh

Backspace String Compare

Jan 17th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.
  2.  
  3. Note that after backspacing an empty text, the text will continue empty.
  4.  
  5. class Solution {
  6. public:
  7. bool backspaceCompare(string s, string t)
  8. {
  9. stack<char>s1,t1;
  10. for(int i=0;i<s.size();i++)
  11. {
  12. if(s[i]=='#')
  13. {
  14. if(!s1.empty())
  15. {
  16. s1.pop();
  17. }
  18. }
  19. else
  20. {
  21. s1.push(s[i]);
  22. }
  23. }
  24. for(int i=0;i<t.size();i++)
  25. {
  26. if(t[i]=='#')
  27. {
  28. if(!t1.empty())
  29. {
  30. t1.pop();
  31. }
  32. }
  33. else
  34. {
  35. t1.push(t[i]);
  36. }
  37. }
  38. if(s1==t1)
  39. {
  40. return true;
  41. }
  42. return false;
  43. }
  44. };
Advertisement
Add Comment
Please, Sign In to add comment