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