给你一个仅由 大写 英文字符组成的字符串 s
。
你可以对此字符串执行一些操作,在每一步操作中,你可以从 s
中删除 任一个 "AB"
或 "CD"
子字符串。
通过执行操作,删除所有 "AB"
和 "CD"
子串,返回可获得的最终字符串的 最小 可能长度。
注意,删除子串后,重新连接出的字符串可能会产生新的 "AB"
或 "CD"
子串。
示例 1:
输入:s = "ABFCACDB" 输出:2 解释:你可以执行下述操作: - 从 "ABFCACDB" 中删除子串 "AB",得到 s = "FCACDB" 。 - 从 "FCACDB" 中删除子串 "CD",得到 s = "FCAB" 。 - 从 "FCAB" 中删除子串 "AB",得到 s = "FC" 。 最终字符串的长度为 2 。 可以证明 2 是可获得的最小长度。
示例 2:
输入:s = "ACBBD" 输出:5 解释:无法执行操作,字符串长度不变。
提示:
1 <= s.length <= 100
s
仅由大写英文字母组成
提示 1
Can we use brute force to solve the problem?
提示 2
Repeatedly traverse the string to find and remove the substrings “AB” and “CD” until no more occurrences exist.
解法1: 栈
因为题目要求需要删除重新拼接的字符串产生的 “AB" 和 “CD",因此我们可以使用栈。当栈顶两个元素分别为 “AB" 或 “CD" 时,我们通过将它们弹出栈来模拟删除的过程。这样以来,后续的判断过程就是建立在已经删除元素的栈之上了。遍历完之后,返回栈的长度。
Java版:
class Solution {
public int minLength(String s) {
Deque<Character> stack = new LinkedList<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (!stack.isEmpty() && ((ch == 'B' && stack.peek() == 'A')|| (ch == 'D' && stack.peek() == 'C')) ) {
stack.pop();
} else {
stack.push(ch);
}
}
return stack.size();
}
}
Python3版:
class Solution:
def minLength(self, s: str) -> int:
stack = []
for ch in s:
if len(stack) > 0 and ((ch == 'B' and stack[-1] == 'A') or (ch == 'D' and stack[-1] == 'C')):
stack.pop()
else:
stack.append(ch)
return len(stack)
复杂度分析
- 时间复杂度:O(n),其中 n 是字符串 s 的长度。只会遍历 s 一次。
- 空间复杂度:O(n)。是栈的空间复杂度。