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.
staticintpalindromHalfLength(inti,Strings){intstep=1;charpalChar=s.charAt(i-1);while(i-step>=0&&i+step<s.length()&&s.charAt(i-step)==palChar&&s.charAt(i+step)==palChar)step++;returnstep-1;}// Complete the substrCount function below.staticlongsubstrCount(intn,Strings){if(s.length()==0)return0;longres=1;intcountSimChar=1;for(inti=1;i<s.length();i++){if(s.charAt(i-1)==s.charAt(i)){countSimChar++;}else{res+=palindromHalfLength(i,s);countSimChar=1;}res+=countSimChar;}returnres;}
If there are k similar characters in a raw, number of palindroms will be 1+2+..+k;
If current character differs from the prvious one it might also be a palindrom, and half length of the palindrom would be the number of palindroms: aaacaaa -> half length is 3 and there are 3 palindroms: aca, aacaa, aaacaaa
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 →
If there are k similar characters in a raw, number of palindroms will be 1+2+..+k; If current character differs from the prvious one it might also be a palindrom, and half length of the palindrom would be the number of palindroms: aaacaaa -> half length is 3 and there are 3 palindroms: aca, aacaa, aaacaaa