[LeetCode] 844. Backspace String Compare
Given two strings s
and t
, return true
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 = "b" Output: false Explanation: s becomes "c" while t becomes "b".
Constraints:
1 <= s.length, t.length <= 200
s
andt
only contain lowercase letters and'#'
characters.
Follow up: Can you solve it in O(n)
time and O(1)
space?
比较含退格的字符串。
给定
s
和t
两个字符串,当它们分别被输入到空白的文本编辑器后,如果两者相等,返回true
。#
代表退格字符。注意:如果对空文本输入退格字符,文本继续为空。
题意是给两个字符串,中间包含'#',井字的意思是需要退格,请判断在处理完井字之后,两个字符串是否相等。
这个题比较直观的思路是用 stack 做。followup 是会问如何做到不用额外空间,做法是反向遍历,从字符串的尾部遍历到头部。
stack实现,做法应该很直观了,我直接给代码,这里 stack 我用 Deque 实现。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public boolean backspaceCompare(String s, String t) { 3 Deque<Character> queue1 = new ArrayDeque<>(); 4 Deque<Character> queue2 = new ArrayDeque<>(); 5 for (char c : s.toCharArray()) { 6 if (c == '#') { 7 queue1.pollLast(); 8 } else { 9 queue1.offer(c); 10 } 11 } 12 13 for (char c : t.toCharArray()) { 14 if (c == '#') { 15 queue2.pollLast(); 16 } else { 17 queue2.offer(c); 18 } 19 } 20 21 StringBuilder sb1 = new StringBuilder(); 22 while (!queue1.isEmpty()) { 23 sb1.append(queue1.poll()); 24 } 25 StringBuilder sb2 = new StringBuilder(); 26 while (!queue2.isEmpty()) { 27 sb2.append(queue2.poll()); 28 } 29 return sb1.toString().equals(sb2.toString()); 30 } 31 }
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 sb1 = new StringBuilder(); 4 StringBuilder sb2 = 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 sb1.append(c); 18 } 19 } 20 21 for (int i = t.length() - 1; i >= 0; i--) { 22 char c = t.charAt(i); 23 if (c == '#') { 24 tskip++; 25 continue; 26 } 27 if (tskip > 0) { 28 tskip--; 29 continue; 30 } else { 31 sb2.append(c); 32 } 33 } 34 return sb1.toString().equals(sb2.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 };
相关题目