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.
After this, my code passed all tests. I really like the logic. The for inside for actually could be done with just one for loop. Here is my relevant JAVA solution. It passed all the tests.
static class Point{
char text;
long counter;
Point(char t, long c){
text = t;
counter = c;
}
}
// Complete the substrCount function below.
static long substrCount(int n, String s) {
long palindromeCount = 0L;
long equalCounter = 1L;
List<Point> countList = new ArrayList<Point>();
for(int i=1; i<s.length();i++){
if(s.charAt(i)==s.charAt(i-1)){
equalCounter++;
}
else{
countList.add(new Point(s.charAt(i-1),equalCounter));
equalCounter = 1L;
}
}
countList.add(new Point(s.charAt(s.length()-1), equalCounter));
for(int i=0; i<countList.size(); i++){
palindromeCount += (countList.get(i).counter+1)*countList.get(i).counter/2;
}
for (int i = 1; i < countList.size()-1; i++) {
if(countList.get(i).counter == 1
&& countList.get(i-1).text == countList.get(i+1).text){
palindromeCount += Math.min(countList.get(i-1).counter,countList.get(i+1).counter);
}
}
return palindromeCount;
}
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 →
After this, my code passed all tests. I really like the logic. The for inside for actually could be done with just one for loop. Here is my relevant JAVA solution. It passed all the tests.
static class Point{ char text; long counter;