From 966d2419ee21f3d798b5316efc949768b44e8396 Mon Sep 17 00:00:00 2001 From: akshat235 <72159377+akshat235@users.noreply.github.com> Date: Fri, 2 Oct 2020 01:40:09 +0530 Subject: [PATCH] Added sherlock-and-valid-string.py Added sherlock_and_valid_string problem from hackerrank --- HackerRank/sherlock-and-valid-string.py | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 HackerRank/sherlock-and-valid-string.py diff --git a/HackerRank/sherlock-and-valid-string.py b/HackerRank/sherlock-and-valid-string.py new file mode 100644 index 0000000..a4a03d9 --- /dev/null +++ b/HackerRank/sherlock-and-valid-string.py @@ -0,0 +1,59 @@ +'''URL = https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem''' +#Question +''' +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. +''' + +#ANSWERS +import math +import os +import random +import re +import sys + +# Completing the isValid function below. +def isValid(s): + + char_dict = {} + for char in s: + if char in char_dict: + char_dict[char] += 1 + else: + char_dict[char] = 1 + #initiated largest and smallest count with last char + min_count = char_dict[char] + max_count = char_dict[char] + # counting how many times a count occured + count_dict = {} + for char, value in char_dict.items(): + if value in count_dict: + count_dict[value] += 1 + else: + count_dict[value] = 1 + #updated max and min count + if value < min_count: + min_count = value + if value > max_count: + max_count = value + # final test: + if len(count_dict) == 1: + return 'YES' + elif len(count_dict) == 2: + if count_dict[max_count] == 1 and max_count - min_count == 1: + return 'YES' + elif count_dict[min_count] == 1 and min_count == 1: + return 'YES' + return 'NO' + +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + s = input() + + result = isValid(s) + + fptr.write(result + '\n') + + fptr.close() \ No newline at end of file