You are viewing a single comment's thread. Return to all comments →
time complexity: O(n):
if(len(s)>2): dic = {} for i in s: if(i in dic): dic[i]+=1 else: dic[i]=1
count =0 dicy = list(dic.keys()) j=0 cptr=0 nptr=1 while(count<2 and j<len(dicy)-1): nxt=dic[dicy[nptr]] curr=dic[dicy[cptr]] if(curr!=nxt): if(abs(nxt-curr)==1): count+=1 nptr+=1 else: if(curr==1 or nxt==1): count+=1 else: return 'NO' else: nptr+=1 j+=1 return 'NO' if count>1 else 'YES' else: return 'YES'
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and the Valid String
You are viewing a single comment's thread. Return to all comments →
time complexity: O(n):
if(len(s)>2): dic = {} for i in s: if(i in dic): dic[i]+=1 else: dic[i]=1