-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsherlock_and_valid_string.py
More file actions
executable file
·42 lines (33 loc) · 1.13 KB
/
sherlock_and_valid_string.py
File metadata and controls
executable file
·42 lines (33 loc) · 1.13 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
38
39
40
41
42
#!/usr/bin/env python3
import os
import sys
from collections import defaultdict
from pathlib import Path
from typing import IO
def isValid(s: str) -> bool:
"""
Check if string is valid.
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 character at 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.
"""
counts = defaultdict(int)
for char in s:
counts[char] += 1
counts = sorted(counts.values())
return \
(counts[0] == counts[-1]) or \
(counts[0] == 1 and counts[1] == counts[-1]) or \
(counts[0] == counts[-2] == counts[-1] - 1)
def main(fptr: IO) -> None:
s = input()
result = isValid(s)
fptr.write(("YES" if result else "NO") + "\n")
if __name__ == "__main__":
if path := os.getenv("OUTPUT_PATH"):
with Path(path).open("wt", encoding="utf-8") as fptr:
main(fptr)
fptr.close()
else:
main(sys.stdout)