Brute force C++ (~6ms)


  • 0
    M
        int countSubstrings(string s) {
            auto N = static_cast<int>(s.size());
    
            auto palicount = [&](int lo, int hi) {
                int cnt = 0;
                for (; (0 <= lo) && (hi < N); cnt++)
                    if (s[lo--] != s[hi++])
                        break;
                return cnt;
            };
    
            int res = 0;
            for (int i = 0; i < N; ++i) {
                res += palicount(i, i);
                res += palicount(i - 1, i);
            }
    
            return res;
        }
    

Log in to reply
 

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