Re: [闲聊] 每日leetcode

楼主: dont   2024-07-30 22:44:03
1653. Minimum Deletions to Make String Balanced
## 思路
Minimum Deletions = 把所有的ba配对删掉的次数
for循环扫整个字串
如果遇到a 且Stack最后是b 就pop掉 并且 ans+1
否然就把该字符加进Stack
## Complexity
Time, Space: O(N)
## Code
```python
class Solution:
def minimumDeletions(self, s: str) -> int:
n = len(s)
ans = 0
stack = []
for ch in s:
if stack and stack[-1] == 'b' and ch == 'a':
stack.pop()
ans += 1
else:
stack.append(ch)
return ans
```
作者: oin1104 (是oin的说)   2024-07-30 23:15:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com