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?
给2个字符串S和T,里面含有#代表退格键,意味着前面的字符会被删除,判断2个字符串是否相等,字符串中只含有小写字母和#。
解法1:用一个栈存字符,循环字符串,如果是字母就加入栈,遇到#而且栈里面有字母就pop出栈里最后的字符。T: O(n), S:(n)
解法2:follow up要求O(N) time and O(1) space,在一个循环里,从后往前处理字符串,用一个变量记录要删除的字符数量,遇到#时变量加1,遇到字符并且变量大于1,变量减1,直到没遇到#并且要变量为0,这时比较两个字符此时是否一样,不一样返回false,如果字符串比较完没有不一样的字符出现,返回ture。
G家:follow up: 如果有大写键CAP
Java: O(1) space
public boolean backspaceCompare(String S, String T) {
int i = S.length() - 1, j = T.length() - 1;
while (true) {
for (int back = 0; i >= 0 && (back > 0 || S.charAt(i) == '#'); --i)
back += S.charAt(i) == '#' ? 1 : -1;
for (int back = 0; j >= 0 && (back > 0 || T.charAt(j) == '#'); --j)
back += T.charAt(j) == '#' ? 1 : -1;
if (i >= 0 && j >= 0 && S.charAt(i) == T.charAt(j)) {
i--; j--;
} else
return i == -1 && j == -1;
}
}

Python:

 def backspaceCompare(self, S, T):
i, j = len(S) - 1, len(T) - 1
backS = backT = 0
while True:
while i >= 0 and (backS or S[i] == '#'):
backS += 1 if S[i] == '#' else -1
i -= 1
while j >= 0 and (backT or T[j] == '#'):
backT += 1 if T[j] == '#' else -1
j -= 1
if not (i >= 0 and j >= 0 and S[i] == T[j]):
return i == j == -1
i, j = i - 1, j - 1

Python:

# Time:  O(m + n)
# Space: O(1)
import itertools class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
def findNextChar(S):
skip = 0
for i in reversed(xrange(len(S))):
if S[i] == '#':
skip += 1
elif skip:
skip -= 1
else:
yield S[i] return all(x == y for x, y in
itertools.izip_longest(findNextChar(S), findNextChar(T)))

Python: wo O(n) space

class Solution(object):
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
s1, s2 = [], []
for i in range(len(S)):
if S[i] == '#' and s1:
s1.pop()
elif S[i] == '#':
continue
else:
s1.append(S[i]) for j in range(len(T)):
if T[j] == '#' and s2:
s2.pop()
elif T[j] == '#':
continue
else:
s2.append(T[j]) return s1 == s2

C++:  

bool backspaceCompare(string S, string T) {
int i = S.length() - 1, j = T.length() - 1;
while (1) {
for (int back = 0; i >= 0 && (back || S[i] == '#'); --i)
back += S[i] == '#' ? 1 : -1;
for (int back = 0; j >= 0 && (back || T[j] == '#'); --j)
back += T[j] == '#' ? 1 : -1;
if (i >= 0 && j >= 0 && S[i] == T[j])
i--, j--;
else
return i == -1 && j == -1;
}
}

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 844. Backspace String Compare 退格字符串比较的更多相关文章

  1. [LeetCode] Backspace String Compare 退格字符串比较

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  2. 【Leetcode_easy】844. Backspace String Compare

    problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...

  3. 【LeetCode】844. Backspace String Compare 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...

  4. [LeetCode&Python] Problem 844. Backspace String Compare

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  5. 844. Backspace String Compare判断删除后的结果是否相等

    [抄题]: Given two strings S and T, return if they are equal when both are typed into empty text editor ...

  6. 844. Backspace String Compare

    class Solution { public: bool backspaceCompare(string S, string T) { int szs=S.size(); int szt=T.siz ...

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

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  8. LeetCode:比较含退格字符串【844】

    LeetCode:比较含退格字符串[844] 题目描述 给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = ...

  9. C#LeetCode刷题之#844-比较含退格的字符串​​​​​​​(Backspace String Compare)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4030 访问. 给定 S 和 T 两个字符串,当它们分别被输入到空 ...

随机推荐

  1. Gorgeous Sequence(HDU5360+线段树)

    题目链接 传送门 题面 思路 对于线段树的每个结点我们存这个区间的最大值\(mx\).最大值个数\(cnt\).严格第二大数\(se\),操作\(0\): 如果\(mx\leq val\)则不需要更新 ...

  2. Uva1349Optimal Bus Route Design(二分图最佳完美匹配)(最小值)

    题意: 给定n个点的有向图问,问能不能找到若干个环,让所有点都在环中,且让权值最小,KM算法求最佳完美匹配,只不过是最小值,所以把边权变成负值,输出时将ans取负即可 这道题是在VJ上交的 #incl ...

  3. 弹性盒模型:flex多行多列两端对齐,列不满左对齐

    [1]需求: [2]解决方案: 最近遇到布局上要求item两端对齐,且最后一行在列不满的情况下要求左对齐,使用flex的justify-content: space-between;实现时发现最后一行 ...

  4. WPF 控件库——带有惯性的ScrollViewer*(转)

    转:https://blog.csdn.net/ahilll/article/details/82418892 一.先看看效果 二.原理 虽然效果很简单,但是网上的一些资料涉及的代码量非常可观,而且效 ...

  5. 从输入URL到页面返回的过程详解

    文章转自以为大神的博客;https://www.cnblogs.com/xianyulaodi/p/6547807.html#_labelTop 总结的很不错,看完收获颇多, 下面就是大神的文章,我只 ...

  6. C# Base64字符串生成图片

    C# Base64字符串生成图片: //签字图片Base64格式去除开头多余字符data:image/png;base64, strSignImg = strSignImg.Substring(str ...

  7. 输入一个正整数n,生成一张2的乘方表,输出2*0—2*n的值。

    #include<stdio.h>#include<math.h> //程序中调用幂函数pow(),需包含头文件math.h//void main(){ int i,n; pr ...

  8. 【批处理】choice命令,call 命令,start 命令,rem

    [1]choice命令简介 使用此命令可以提示用户输入一个选择项,根据用户输入的选择项再决定执行具体的过程. 使用时应该加/c:参数,c: 后应写提示可输入的字符或数字,之间无空格.冒号是可选项. 使 ...

  9. Codevs 1305 Freda的道路(矩阵乘法 DP优化)

    1305 Freda的道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description Freda要到Rainbow的城堡去玩了.我们可以认 ...

  10. HTML之微信全屏播放视频

    不废话,上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...