Special String Again

  • + 0 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;

        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;
    }