-
Notifications
You must be signed in to change notification settings - Fork 222
/
sherlock-and-valid-string.py
48 lines (37 loc) · 1.04 KB
/
sherlock-and-valid-string.py
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
38
39
40
41
42
43
44
45
46
47
48
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
# Complete the isValid function below.
def isValid(s):
cnt = Counter(s)
res = 'NO'
print("cnt = {} len = {}".format(cnt, len(set(cnt.values()))))
if len(set(cnt.values())) == 1:
res = 'YES'
elif len(set(cnt.values())) == 2:
bigger = max(cnt.values())
lesser = min(cnt.values())
bigger_let = [let for let, c in cnt.items() if c == bigger]
lesser_let = [let for let, c in cnt.items() if c == lesser]
if len(lesser_let) == 1 and lesser == 1:
res = 'YES'
elif len(bigger_let) == 1 or len(lesser_let) == 1:
if abs(bigger-lesser) == 1:
res = 'YES'
else:
res = 'NO'
else:
res = 'NO'
else:
res = 'NO'
return res
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
result = isValid(s)
fptr.write(result + '\n')
fptr.close()