Java Simple Solution

  • 0
    class Solution {
        public int countSubstrings(String s) {
            Map<Integer, Character> map = new HashMap<>();
            int ans = s.length();
            int endChar;
            Boolean isPalindrome = true;
            // Go through the string on char at a time
            for (int i=0; i < s.length(); i ++) {
                map.put(i, s.charAt(i));
                //If there is a possible palindrome then it'll be the second appearance of the character 
                    //Loop through the chars up to that current one we're on
                    for(int j=0; j < i; j++){
                        //reseting the palindrome boolean
                        isPalindrome = true;
                        //setting the end character
                        endChar = i;
                        // iteration through the similar letters
                        if(s.charAt(j) == s.charAt(i)){
                            //looping inwards checking for palindrome
                            for(int k=j+1; k < endChar; k++){
                                endChar --;
                                if(s.charAt(k) != s.charAt(endChar)){
                                    isPalindrome = false;
                                    k = endChar;
                            if (isPalindrome) {
            return ans;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.