LeetCode 844 题解

https://leetcode.com/problems/backspace-string-compare/description/

题目大意:#代表回退键,问两个字符串最后是否一样

解题思路:模拟

class Solution {
    private String fun(String s) {
        StringBuilder S = new StringBuilder(s);
        StringBuilder res=new StringBuilder();
        for(int i=0;i<S.length();i++)
        {
            if(S.charAt(i)=='#')
            {
                if(res.length()>0)
                {
                    res.deleteCharAt(res.length()-1);
                }
            }
            else
                res.append(S.charAt(i));
        }
        return res.toString();
    }
    public boolean backspaceCompare(String S, String T) {
        return fun(S).equals(fun(T));
    }
}

猜你喜欢

转载自blog.csdn.net/u011439455/article/details/80570711