Passion and Patience

Work Hard, Play Hard

导航

844 比较含退格的字符串

class Solution {
    public boolean backspaceCompare(String s, String t) {
        Stack<Integer> stk1 = new Stack<Integer>();
        Stack<Integer> stk2 = new Stack<Integer>(); 
        comP(stk1,s);
        comP(stk2,t);
        return stk1.equals(stk2);
    }

        static void comP(Stack stk,String s){            
            for(int i = 0;i<s.length();i++){
                char ch = s.charAt(i);
                if(ch!='#'){
                    stk.push(ch);
                }else if(!stk.empty()){
                    stk.pop();
                }            
            }
        }
}

posted on 2024-05-20 17:59  安静的聆  阅读(3)  评论(0编辑  收藏  举报