LeetCode-4.9-美区-844-E-比较含退格的字符串(Backspace String Compare)


Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果。 # 代表退格字符。

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 <= S.length <= 200
1 <= T.length <= 200
S and T only contain lowercase letters and ‘#’ characters.

Follow up:
Can you solve it in O(N) time and O(1) space?

思路

(1)参考:比较含退格的字符串力扣 (LeetCode)
(2)自己也想到后指针的做法,包括双指针,但是,想的时候没有这么清晰。利用变量加减和while循环来控制,跳过的元素;
(3)后续的条件控制现在还不是很理解。
(4)似乎栈的解法更容易理解哦

解法1-逆序指针

在这里插入图片描述

class Solution {
    public boolean backspaceCompare(String S, String T) {
        int i = S.length()-1;
        int j = T.length()-1;
        int s = 0;
        int t = 0;
        
        while( i >= 0 || j >= 0){
            
            while(i >= 0){
                if(S.charAt(i) == '#'){
                    i--;
                    s++;
                }else if(s > 0){
                    i--;
                    s--;
                }else{
                    break;
                }
            }
            
            while(j >= 0){
                if(T.charAt(j) == '#'){
                    j--;
                    t++;
                }else if(t > 0){
                    j--;
                    t--;
                }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;
    }
}

解法2-栈

(1)易错点注意
在这里插入图片描述

class Solution {
    public boolean backspaceCompare(String S, String T) {
        char[] s = S.toCharArray();
        char[] t = T.toCharArray();
        int len1 = s.length;
        int len2 = t.length;
        Stack<Character> stack1 = new Stack<>();
        Stack<Character> stack2 = new Stack<>();

        for(Character s1:s){
            if( s1=='#'){
                if(!stack1.isEmpty()){ //error1
                      stack1.pop();
                }
            }else{
                stack1.push(s1);
            }
        }

        for(Character t1:t){
            if(t1=='#'){
                  if(!stack2.isEmpty()){
                      stack2.pop();
                }
            }else{
                stack2.push(t1);
            }
        }

        while(!stack1.isEmpty() && !stack2.isEmpty() ){
            if(stack1.peek() != stack2.peek()){ //error2
                return false;
            }else{
                stack1.pop();
                stack2.pop();
            }
        }

        if(!stack1.isEmpty() || !stack2.isEmpty()){ //error3
            return false;
        }

        return true;
    }
}
发布了194 篇原创文章 · 获赞 20 · 访问量 7939

猜你喜欢

转载自blog.csdn.net/Xjheroin/article/details/105432542