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.
Super-cool! I used it (together with some luck that allowed me to find more small and meaningful test cases) to debug a similar idea I had, only that it works strictly forward rather than expanding from the middle:
staticlongsubstrCount(finalintn,finalStrings){longspecials=n;inti=0,j=1;while(j<n){if(s.charAt(i)!=s.charAt(j)){// Different char and maybe middle char of special substringfinalintrepeatedCharStringLen=j-i;// All len 2+ substrings of same-char string with fixed start are specialspecials+=repeatedCharStringLen-1;// If there is a mirrored same-char substring after the different char, then specialfinalintnewStringAfterSpecialIdx=j+1+repeatedCharStringLen;if(j+1<n&&newStringAfterSpecialIdx<=n&&s.substring(i,j).equals(s.substring(j+1,newStringAfterSpecialIdx))){specials++;}// Advancei++;j=i;}// Expand substring to the rightj++;}specials+=substringsInLen(j-i)-(j-i);// All len 2+ substringsreturnspecials;}privatestaticlongsubstringsInLen(finalintlen){if(len<=0)return0;returnlen*(len+1)/2;}
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 →
Super-cool! I used it (together with some luck that allowed me to find more small and meaningful test cases) to debug a similar idea I had, only that it works strictly forward rather than expanding from the middle: