leetcode刷题(844)——回退字符串比较

这篇博客探讨了LeetCode上的一道题目,涉及字符串比较问题,其中回退字符'#'起到了关键作用。文章指出,由于要求空间复杂度为O(1),所以不能简单地使用栈来解决,而应从后往前遍历,遇到 '#' 时移动指针。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

Example 1:

Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".

Example 2:

Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".

Example 3:

Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".

Example 4:

Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".

Note:

  1. 1 <= S.length <= 200
  2. 1 <= T.length <= 200
  3. S and T only contain lowercase letters and '#' characters.

Follow up:

  • Can you solve it in O(N) time and O(1) space?

很直接的想到用栈来解决(如下),但是题目要求空间复杂度为O(1),所以只能使用其他方法。注意到#只对前面一个字符产生影响,所以从后往前看就是遇到#就把角标移动一个。

class Solution {
    public boolean backspaceCompare(String S, String T) {
        Stack<Character> stack1 = new Stack<>();
        Stack<Character> stack2 = new Stack<>();        
        char c;       
        for(int i=0;i<S.length();i++){
            c = S.charAt(i);
            if(c!='#'){
                stack1.push(c);
            }else{
                if(!stack1.isEmpty()){
                    stack1.pop();
                }
            }
        }
        for(int i=0;i<T.length();i++){
            c = T.charAt(i);
            if(c!='#'){
                stack2.push(c);
            }else{
                if(!stack2.isEmpty()){
                    stack2.pop();
                }
            }
        }
        while(!stack1.isEmpty()&&!stack2.isEmpty()){
            if(stack1.pop()!=stack2.pop())
                return false;
        }
        return stack1.isEmpty()&&stack2.isEmpty();
    }
}

 

class Solution {
    public boolean backspaceCompare(String S, String T) {
        int i = S.length()-1;  
        int j = T.length()-1;  
        int skipi = 0;   //记录#数量
        int skipj = 0;  //记录#数量
        
        while(i>=0||j>=0){
            while(i>=0){
                if(S.charAt(i)=='#'){
                    skipi++;  
                    i--;  //碰到#号,往前移动一个
                }else if(skipi>0){ //上面只移动一个而不是直接移动两个的原因就是有可能两个#相连
                    i--;
                    skipi--;
                }else{
                    break;
                }
            }
            while(j>=0){
                if(T.charAt(j)=='#'){
                    skipj++;
                    j--;
                }else if(skipj>0){
                    j--;
                    skipj--;
                }else{
                    break;
                }
            }
            if(i>=0&&j>=0&&S.charAt(i)!=T.charAt(j))
                return false;
            if((i>=0)!=(j>=0))
                return false;
            i--;
            j--;
        }
        return true;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值