Change Minimum Characters to Satisfy One of Three Conditions LeetCode solution in Python

1
class Solution:
2
def minCharacters(self, a: str, b: str) -> int:
3
pa, pb = [0] * 26, [0] * 26
4
for x in a:
5
pa[ord(x) - 97] += 1
6
for x in b:
7
pb[ord(x) - 97] += 1
8

9
ans = len(a) - max(pa) + len(b) - max(pb) # condition 3
10
for i in range(25):
11
pa[i + 1] += pa[i]
12
pb[i + 1] += pb[i]
13
ans = min(ans, pa[i] + len(b) - pb[i]) # condition 2
14
ans = min(ans, len(a) - pa[i] + pb[i]) # condition 1
15
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0