Given two strings S
and T
, return if they are equal when both are typed into empty text editors. #
means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
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
andT
only contain lowercase letters and'#'
characters.
Follow up:
- Can you solve it in
O(N)
time andO(1)
space?
比较含退格的字符串。题意是给两个字符串,中间包含'#',井字的意思是需要退格,请判断在处理完井字之后,两个字符串是否相等。
这个题比较直观的思路是用stack做。followup有可能是会问如何做到不用额外空间,做法是反向遍历,从字符串的尾部遍历到头部。
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 };
反向扫描
反向扫描的思路是,从字符串的尾部遍历到头部,记录一个变量skip,当遇到#的时候,skip++,同时用continue的办法略过当前这个#;之后再看skip,如果skip > 0,则说明需要跳过当前这个字符,就再continue。如果skip == 0则直接append当前字符。
时间O(n)
空间O(1)
Java实现
1 class Solution { 2 public boolean backspaceCompare(String S, String T) { 3 StringBuilder ss = new StringBuilder(); 4 StringBuilder tt = new StringBuilder(); 5 int sskip = 0; 6 int tskip = 0; 7 for (int i = S.length() - 1; i >= 0; i--) { 8 char c = S.charAt(i); 9 if (c == '#') { 10 sskip++; 11 continue; 12 } 13 if (sskip > 0) { 14 sskip--; 15 continue; 16 } else { 17 ss.append(c); 18 } 19 } 20 21 for (int j = T.length() - 1; j >= 0; j--) { 22 char v = T.charAt(j); 23 if (v == '#') { 24 tskip++; 25 continue; 26 } 27 if (tskip > 0) { 28 tskip--; 29 continue; 30 } else { 31 tt.append(v); 32 } 33 } 34 return ss.toString().equals(tt.toString()); 35 } 36 }
JavaScript实现
1 /** 2 * @param {string} S 3 * @param {string} T 4 * @return {boolean} 5 */ 6 var backspaceCompare = function (S, T) { 7 let sskip = 0; 8 let tskip = 0; 9 let sb1 = []; 10 let sb2 = []; 11 for (let i = S.length - 1; i >= 0; i--) { 12 let c = S.charAt(i); 13 if (c === '#') { 14 sskip++; 15 continue; 16 } 17 if (sskip > 0) { 18 sskip--; 19 continue; 20 } else { 21 sb1.push(c); 22 } 23 } 24 25 for (let j = T.length - 1; j >= 0; j--) { 26 let v = T.charAt(j); 27 if (v === '#') { 28 tskip++; 29 continue; 30 } 31 if (tskip > 0) { 32 tskip--; 33 continue; 34 } else { 35 sb2.push(v); 36 } 37 } 38 return sb1.toString() === sb2.toString(); 39 };
相关题目