Hackerrank — Sherlock and the Valid String walkthrough #Python #Hackerrank

Codewhy
1 min readMar 8, 2022

--

Hi all, attached below is the code demo for a Hackerrank that I recently attempted. Detailed Walkthrough explaining my steps to solve and discuss Hackerrank Qn Sherlock and the Valid String can be found on YouTube.

Youtube Walkthrough

The question is of Medium difficulty. With concepts such as ASCII key, filter, sorted and a bunch of others

Python Code

def isValid(s):
arr = [0]*26
for i in s:
asc =ord(i)-97
arr[asc] +=1
final = sorted(list(filter(lambda x :x!=0, arr)))
if len(set(final))==1:
return "YES"
elif final[0]==1 and len(set(final[1:]))==1:
return "YES"
final[-1] -=1
if len(set(final))==1:
return "YES"
else:
return "NO"
Easier on the eyes

Time Complexity — O(N)

Hope you found it interesting and if you would like to see more, please show some support :)

~Codewhy

Tags

sherlock and the valid string hackerrank python,sherlock and the valid string hackerrank solution,sherlock and the valid string python,sherlock and the valid string solution c++,sherlock and the valid string in java,sherlock and the valid string solution,sherlock and the valid string java solution,sherlock and the valid string,how to solve sherlock and the valid string,hackerrank solution,valid string,hackerrank,hackers realm,sherlock,programming,problem solving,python

--

--

Codewhy

CodeWhy? No really… why? I am keen on posting about fintech, machine learning, Stocks 🚀 and many more. Lets see how it goes … Lets learn together bois n gals