[LeetCode] 844. Backspace String Compare

Posted aaronliu1991

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 844. Backspace String Compare相关的知识,希望对你有一定的参考价值。

比较含退格的字符串。题意是给两个字符串,中间包含井字,井字的意思是需要退格,请判断两个字符是否相等。例子,

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".

这个题比较简单,比较直观的思路是用stack做。followup有可能是会问如何做到不用额外空间,这里需要用到two pointer的做法。遍历的时候,需要从input的尾部开始遍历到头部,遇到井号的时候就记录一个skip++,如果不是井号但是skip大于0的话,就再i--;当skip为0的时候,会跳出while循环,这时比较S和T当前的char是否一样,若不一样则return false。如果两者有一个先遍历完了,也return false。

stack实现

时间O(n)

空间O(n)

Java

 1 class Solution {
 2     public boolean backspaceCompare(String S, String T) {
 3         return build(S).equals(build(T));
 4     }
 5 
 6     public String build(String S) {
 7         Stack<Character> stack = new Stack();
 8         for (char c : S.toCharArray()) {
 9             if (c != ‘#‘) {
10                 stack.push(c);
11             } else if (c == ‘#‘ && !stack.empty()) {
12                 stack.pop();
13             }
14         }
15         return String.valueOf(stack);
16     }
17 }

 

javascript

 1 /**
 2  * @param {string} S
 3  * @param {string} T
 4  * @return {boolean}
 5  */
 6 var backspaceCompare = function(S, T) {
 7     return helper(S) == helper(T);
 8 };
 9 
10 var helper = function(input) {
11     let stack = [];
12     for (let c of input) {
13         if (c !== ‘#‘) {
14             stack.push(c);
15         } else if (c == ‘#‘ && stack.length > 0) {
16             stack.pop();
17         }
18     }
19     return stack.join(‘‘);
20 };

 

two pointer实现

时间O(n)

空间O(1)

Java

 1 class Solution {
 2     public boolean backspaceCompare(String S, String T) {
 3         int i = S.length() - 1;
 4         int j = T.length() - 1;
 5         int skipS = 0;
 6         int skipT = 0;
 7 
 8         while (i >= 0 || j >= 0) { // While there may be chars in build(S) or build (T)
 9             while (i >= 0) { // Find position of next possible char in build(S)
10                 if (S.charAt(i) == ‘#‘) {
11                     skipS++;
12                     i--;
13                 } else if (skipS > 0) {
14                     skipS--;
15                     i--;
16                 } else {
17                     break;
18                 }
19             }
20             while (j >= 0) { // Find position of next possible char in build(T)
21                 if (T.charAt(j) == ‘#‘) {
22                     skipT++;
23                     j--;
24                 } else if (skipT > 0) {
25                     skipT--;
26                     j--;
27                 } else {
28                     break;
29                 }
30             }
31             // If two actual characters are different
32             if (i >= 0 && j >= 0 && S.charAt(i) != T.charAt(j)) {
33                 return false;
34             }
35             // If expecting to compare char vs nothing
36             if ((i >= 0) != (j >= 0)) {
37                 return false;
38             }
39             i--;
40             j--;
41         }
42         return true;
43     }
44 }

 

JavaScript

 1 /**
 2  * @param {string} S
 3  * @param {string} T
 4  * @return {boolean}
 5  */
 6 var backspaceCompare = function(S, T) {
 7     let i = S.length - 1;
 8     let j = T.length - 1;
 9     let skipS = 0;
10     let skipT = 0;
11     while (i >= 0 || j >= 0) {
12         while (i >= 0) {
13             if (S.charAt(i) == ‘#‘) {
14                 skipS++;
15                 i--;
16             } else if (skipS > 0) {
17                 skipS--;
18                 i--;
19             } else {
20                 break;
21             }
22         }
23 
24         while (j >= 0) {
25             if (T.charAt(j) == ‘#‘) {
26                 skipT++;
27                 j--;
28             } else if (skipT > 0) {
29                 skipT--;
30                 j--;
31             } else {
32                 break;
33             }
34         }
35 
36         if (i >= 0 && j >= 0 && S.charAt(i) !== T.charAt(j)) {
37             return false;
38         }
39         if (i >= 0 !== j >= 0) {
40             return false;
41         }
42         i--;
43         j--;
44     }
45     return true;
46 };

 

以上是关于[LeetCode] 844. Backspace String Compare的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 844. Backspace String Compare

(栈,双指针) leetcode. 844 Backspace String Compare

LeetCode_844-Backspace String Compare

844. Backspace String Compare

[LeetCode] 844. Backspace String Compare_Easy tag: Stack **Two pointers

844. Backspace String Compare