-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_minimumLength.py
More file actions
28 lines (28 loc) · 1.16 KB
/
13_minimumLength.py
File metadata and controls
28 lines (28 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''You are given a string s.
You can perform the following process on s any number of times:
Choose an index i in the string such that there is at least one character to the left of index i that
is equal to s[i], and at least one character to the right that is also equal to s[i].
Delete the closest character to the left of index i that is equal to s[i].
Delete the closest character to the right of index i that is equal to s[i].
Return the minimum length of the final string s that you can achieve.
Example 1:
Input: s = "abaacbcbb"
Output: 5
Explanation:
We do the following operations:
Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb"'''
from typing import List
class Solution:
def minimumLength(self, s: str) -> int:
freq=[0]*26
ans=0
for i in s:
freq[ord(i)-ord('a')]+=1
for i in range(len(freq)):
if freq[i]!=0:
if freq[i]%2==0 :
ans+=2
else:
ans+=1
return ans