题目:
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 <= S.length <= 200
1 <= T.length <= 200
S and T only contain lowercase letters and '#' characters.
Follow up:
Can you solve it in O(N) time and O(1) space?
时间:2019.9.19
分析:本题想做的话还是很简单的。但如果考虑 follow up 的要求:O(N) time and O(1) space ,就不简单了。说实话,一开始我真的没有做出来,没有通过所有的案例,且代码写得比较复杂。
我是从下标 0 开始遍历的,分别赋予字符串下标 i, j,考虑字符串相等,不等或遇到 ‘#’ 等情况,自以为写的蛮好,测试的时候却出现各种之前没有想过的数据。于是发现自己的思路有问题。也许缝缝补补最终也能做出来吧。但这样终究不是好办法。后来看了别人的代码,实在佩服。短短几行代码就能搞定。关键在于从后向前遍历。
代码先不贴了,下次重新做一遍再贴出来。