-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
21 lines (19 loc) · 739 Bytes
/
main.py
File metadata and controls
21 lines (19 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution(object):
def minimumLength(self, s):
"""
:type s: str
:rtype: int
"""
# Step 1: Count the frequency of each character in the string
char_frequency_map = Counter(s)
# Step 2: Calculate the number of characters to delete
delete_count = 0
for frequency in char_frequency_map.values():
if frequency % 2 == 1:
# If frequency is odd, delete all except one
delete_count += frequency - 1
else:
# If frequency is even, delete all except two
delete_count += frequency - 2
# Step 3: Return the minimum length after deletions
return len(s) - delete_count