Skip to content

Commit 608c71a

Browse files
committed
Time: 769 ms (11.67%), Space: 25.2 MB (6%) - LeetHub
1 parent 79e0cc4 commit 608c71a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def minimumDeletions(self, s: str) -> int:
3+
n = len(s)
4+
5+
# Arrays to store prefix and suffix counts
6+
prefix_b_count = [0] * (n + 1)
7+
suffix_a_count = [0] * (n + 1)
8+
9+
# Fill prefix_b_count
10+
for i in range(1, n + 1):
11+
prefix_b_count[i] = prefix_b_count[i - 1] + (1 if s[i - 1] == 'b' else 0)
12+
13+
# Fill suffix_a_count
14+
for i in range(n - 1, -1, -1):
15+
suffix_a_count[i] = suffix_a_count[i + 1] + (1 if s[i] == 'a' else 0)
16+
17+
# Calculate minimum deletions
18+
min_deletions = float('inf')
19+
for i in range(n + 1):
20+
min_deletions = min(min_deletions, prefix_b_count[i] + suffix_a_count[i])
21+
22+
return min_deletions

0 commit comments

Comments
 (0)