We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importosfromcollectionsimportCounterfromtypingimportLiteral## Complete the 'is_valid' function below.## The function is expected to return a STRING.# The function accepts STRING s as parameter.#defis_valid(s:str)->Literal["NO","YES"]:"""Given a string, determine if it is valid."""y="YES"iflen(s)==1:# optionalreturnymatchlen(counts:={*(frequencies:=Counter(s).values())}):case1:returnycase2:meta_freq=Counter(frequencies)lo,hi=sorted(counts)if1==lo==meta_freq[lo]:returnyif1==hi-lo==meta_freq[hi]:returnyreturn"NO"if__name__=="__main__":result=is_valid(input())withopen(os.environ["OUTPUT_PATH"],"w")asfptr:fptr.write(f"{result}\n")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all comments →
Python 3.10+ solution: