Very concise simple C++ solution


  • 0
    S
    int countSubstrings(string s) {
        int count = 0;
        for (int i = 0; i < s.size(); i++) {
            // When length of substring is odd
            for (int j = 0; i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]; j++)
                count++;
            // When length of substring is even
            for (int j = 0; i - j >= 0 && i + j + 1 < s.size() && s[i - j] == s[i + j + 1]; j++)
                count++;
        }
        return count;
    }
    

Log in to reply
 

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