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
andT
only contain lowercase letters and'#'
characters.
Follow up:
- Can you solve it in
O(N)
time andO(1)
space?
题目没看太懂,看完案例看懂了。字符'#'代表退格符,如果在#号前有非#号字符的话就要消去。这样子题目就很浅显易懂了,直接把字符串一个一个对比,如果遇到非#号字符就保存,遇到#号并且#号前有字符的话就消去,最后再比较两个字符串的字符是否相等就行。
1 void modifyString(char* S) { 2 int cur = 0; 3 for(int i=0;S[i]!='