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.
Just another Python3 solution.
Sliding window with tho pointers (left: iterator, right: implicit via for).
I fiddled a little bit with the Counter class to support rich comparision (just my aesthetic preference).
ifsys.version_info[:2]>=(3,10):# Counter supports comparisionfromcollectionsimportCounterelse:# add operator ">=" to class CounterfromcollectionsimportCounterasCounter_origclassCounter(Counter_orig):def__ge__(self,other):returnall(self[k]>=other[k]forkinother.keys())defsteadyGene(gene):baddies=Counter({k:v-len(gene)//4fork,vinCounter(gene).items()ifv>len(gene)//4})ifnotbaddies:return0genecount_win,cur_winsize,min_winsize=Counter(),0,len(gene)left_it=iter(gene)cl=next(left_it)# cl: char at window's left marginforcringene:cur_winsize+=1genecount_win[cr]+=1ifcrnotinbaddies:continuewhileclnotinbaddiesorgenecount_win[cl]>baddies[cl]:cur_winsize-=1genecount_win[cl]-=1cl=next(left_it)ifgenecount_win>=baddiesandcur_winsize<min_winsize:min_winsize=cur_winsizereturnmin_winsize
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bear and Steady Gene
You are viewing a single comment's thread. Return to all comments →
Just another Python3 solution.
Sliding window with tho pointers (left: iterator, right: implicit via for).
I fiddled a little bit with the Counter class to support rich comparision (just my aesthetic preference).