Advertisement
korobushk

backspacecompare

May 23rd, 2021
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. class Solution {
  2.     public boolean backspaceCompare(String s, String t) {
  3.      
  4.         Stack <Character> stacks = new Stack<>();
  5.         Stack <Character> stackT = new Stack<>();
  6.  
  7.         for(Character ss:s.toCharArray()){
  8.             if(ss!='#'){
  9.                 stacks.push(ss);
  10.             }else if (!stacks.isEmpty()){
  11.                 stacks.pop();
  12.             }
  13.         }
  14.  
  15.         for(Character tt:t.toCharArray()){
  16.             if(tt!='#'){
  17.                 stackT.push(tt);
  18.             }else if (!stackT.isEmpty()){
  19.                 stackT.pop();
  20.             }
  21.         }
  22.  
  23.         return stacks.equals(stackT) ;
  24.  
  25.     }
  26. }
  27. //Input: s = "ab#c", t = "ad#c"
  28. //Output: true
  29. //Explanation: Both s and t become "ac".
  30.  
  31. //# deletes letter = backspace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement