forked from nathan-abela/HackerRank-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03 - Sherlock and the Valid String.py
51 lines (37 loc) · 1.2 KB
/
03 - Sherlock and the 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
49
50
51
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem
# Difficulty: Medium
# Max Score: 35
# Language: Python
# ========================
# Solution
# ========================
import os
# Complete the isValid function below.
def isValid(s):
frequency = {}
# Set frequencies
for char in s:
frequency.setdefault(char, 0)
frequency[char] += 1
values = list(frequency.values())
# Not important which number is used, in this case, first will be used
first = values[0]
accumulated_difference = 0
for _ in values:
# Difference is taken into consideration -> abs()
diff = abs(_ - first)
# If the value is 1, without taking into consideration the difference, it is counted as 1
accumulated_difference += diff if _ != 1 else 1
# Stops if the the difference is already more than 1
if accumulated_difference > 1:
return "NO"
return "YES"
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
RESULT = isValid(s)
fptr.write(RESULT + '\n')
fptr.close()