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.
funcsubstrCount(nint32,sstring)int64{varcountint64varcurrChar,prevChar,prevPrevCharint32varcurrLength,prevLength,prevPrevLengthint64fori,char:=ranges{ifi==0{currChar=charcurrLength++continue}ifchar==currChar{currLength++continue}count+=currLength*(currLength+1)/2// for case "aaabaaaab" when we on the second bifcurrChar==prevPrevChar&&prevLength==1{count+=min(prevPrevLength,currLength)}prevPrevChar=prevCharprevPrevLength=prevLengthprevChar=currCharprevLength=currLengthcurrChar=charcurrLength=1}// add the remainingcount+=currLength*(currLength+1)/2ifcurrChar==prevPrevChar&&prevLength==1{count+=min(prevPrevLength,currLength)}returncount}funcmin(a,bint64)int64{ifa<b{returna}else{returnb}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Special String Again
You are viewing a single comment's thread. Return to all comments →
Go