「力扣」第 844 題:比較含退格的字符串(棧、雙指針)

英文版 LeetCode 每日一題(2020 年 4 月 10 日)

地址:https://leetcode-cn.com/problems/backspace-string-compare/

地址:https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3291/

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.

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?

思路一:使用棧

Java 代碼:

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution3 {

    public boolean backspaceCompare(String S, String T) {
        return simplify(S).equals(simplify(T));
    }

    private String simplify(String str){
        Deque<Character> stack = new ArrayDeque<>();
        char[] charArray = str.toCharArray();
        for (char c : charArray) {
            if (c != '#') {
                stack.addLast(c);
            } else if (!stack.isEmpty()) {
                stack.pollLast();
            }
        }
        return stack.toString();
    }

    public static void main(String[] args) {
        String S = "ab#c";
        String T = "ad#c";

//        String S = "ab##";
//        String T = "c#d#";

//        String S = "a##c";
//        String T = "#a#c";

//        String S = "a#c";
//        String T = "b";

        Solution3 solution3 = new Solution3();
        boolean res = solution3.backspaceCompare(S, T);
        System.out.println(res);
    }
}

思路二:雙指針

參考:https://www.cnblogs.com/grandyang/p/10447783.html

我想到了雙指針,但是沒有想到從後向前遍歷,還有具體的細節懶得想了,優化了原作者的代碼。

Java 代碼:

public class Solution {

    public boolean backspaceCompare(String S, String T) {
        char[] charArray1 = S.toCharArray();
        char[] charArray2 = T.toCharArray();

        int len1 = S.length();
        int len2 = T.length();

        // 連續出現 # 的個數
        int cnt1 = 0;
        int cnt2 = 0;

        int i = len1 - 1;
        int j = len2 - 1;

        while (i >= 0 || j >= 0) {

            // 邏輯上應該先處理 # 號

            while (i >= 0 && (charArray1[i] == '#' || cnt1 > 0)) {
                if (charArray1[i] == '#') {
                    cnt1++;
                } else {
                    cnt1--;
                }
                i--;
            }

            while (j >= 0 && (charArray2[j] == '#' || cnt2 > 0)) {
                if (charArray2[j] == '#') {
                    cnt2++;
                } else {
                    cnt2--;
                }
                j--;
            }

            // 這裏處理數組下標越界的情況
            if (i < 0 || j < 0) {
                return i == j;
            }

            // 這是最簡單的情況
            if (charArray1[i] != charArray2[j]) {
                return false;
            }

            // 然後繼續向前遍歷
            j--;
            i--;
        }

        // 細節特別多,到這裏就完了
        return i == j;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章