-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsherlock_valid_string.py
More file actions
37 lines (31 loc) · 1.49 KB
/
Copy pathsherlock_valid_string.py
File metadata and controls
37 lines (31 loc) · 1.49 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
29
30
31
32
33
34
35
36
37
"""
Sherlock considers a string to be valid if all characters of the string appear
the same number of times. It is also valid if he can remove just 1 character at
1 index in the string, and the remaining characters will occur the same number
of times. Given a string, determine if it is valid. If so, return YES,
otherwise return NO.
"""
from collections import Counter
def isValid(s):
count_by_char = Counter(s) # abbac -> {'a': 2, 'b': 2, 'c': 1}
count_by_occurrence_num = Counter(count_by_char.values()) # {2: 2, 1: 1}
count_by_occ_uniq_values = set(count_by_occurrence_num.keys()) # {1,2}
if len(count_by_occ_uniq_values) == 1:
return "YES"
elif len(count_by_occ_uniq_values) == 2:
max_count = max(count_by_occ_uniq_values)
min_count = min(count_by_occ_uniq_values)
# valid case if there's only one char with the biggest occurence, and the diff with the smallest one is 1
if count_by_occurrence_num[max_count] == 1 and max_count - min_count == 1:
return "YES"
# valid case if there's only one char with the smallest occurence, and that occurrence is only 1
if count_by_occurrence_num[min_count] == 1 and min_count == 1:
return "YES"
return "NO"
def test_me():
assert isValid("abc") == "YES"
assert isValid("abcc") == "YES"
assert isValid("aabbcd") == "NO"
assert isValid("abbac") == "YES"
assert isValid("aabbccddeefghi") == "NO"
assert isValid("abcdefghhgfedecba") == "YES"