leet-code/LeetCode/src/main/java/leetcode/editor/cn/BackspaceStringCompare.java

102 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果。 # 代表退格字符。
//
// 注意:如果对空文本输入退格字符,文本继续为空。
//
//
//
// 示例 1
//
//
//输入S = "ab#c", T = "ad#c"
//输出true
//解释S 和 T 都会变成 “ac”。
//
//
// 示例 2
//
//
//输入S = "ab##", T = "c#d#"
//输出true
//解释S 和 T 都会变成 “”。
//
//
// 示例 3
//
//
//输入S = "a##c", T = "#a#c"
//输出true
//解释S 和 T 都会变成 “c”。
//
//
// 示例 4
//
//
//输入S = "a#c", T = "b"
//输出false
//解释S 会变成 “c”但 T 仍然是 “b”。
//
//
//
// 提示:
//
//
// 1 <= S.length <= 200
// 1 <= T.length <= 200
// S 和 T 只含有小写字母以及字符 '#'。
//
//
//
//
// 进阶:
//
//
// 你可以用 O(N) 的时间复杂度和 O(1) 的空间复杂度解决该问题吗?
//
//
//
// Related Topics 栈 双指针
// 👍 276 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//844:比较含退格的字符串
public class BackspaceStringCompare {
public static void main(String[] args) {
//测试代码
Solution solution = new BackspaceStringCompare().new Solution();
solution.backspaceCompare("a##c", "#a#c");
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean backspaceCompare(String S, String T) {
S = getResult(S);
T = getResult(T);
return S.equals(T);
}
private String getResult(String str) {
Stack<Character> stack = new Stack<>();
int length = str.length();
for (char ch : str.toCharArray()) {
if (ch == '#') {
if(!stack.isEmpty()) {
stack.pop();
}
} else {
stack.push(ch);
}
}
str = "";
while (!stack.isEmpty()) {
str += stack.pop().toString();
}
return str;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}