My c++ easy solution


  • 0
    X
    1. use left point and right point
    2. right point to find duplicates
    3. left and right to find palindromic that center of s[i]
    class Solution {
    public:
        int countSubstrings(string s) {
            int len = s.length();
            if(!len) return 0;
            int res = 0;
            for(int i = 0; i < len;i++)
            {
                int left = i - 1;
                int right = i + 1;
                while(right < len && s[right] == s[i])
                {
                    right++;
                    res++;
                }
                while(left >= 0 && right < len && s[left] == s[right])
                {
                    left--;
                    right++;
                    res++;
                }
                res++;;
            }
            return res;
        }
    };
    

Log in to reply
 

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