C++ easy to understand O(n2) answer


  • 0
    J

    class Solution {
    public:
    int countSubstrings(string s) {
    int res(0);
    for(int i = 0;i < s.length();i++) {
    string sub;
    string tmp(s.begin() + i,s.end());
    for(int j = 0;j < tmp.length();j++) {
    sub.push_back(tmp[j]);
    if(test_palind(sub)) {
    res++;
    }
    else continue;
    }
    }
    return res;
    }
    int test_palind(string& sub) {
    int i = 0, j = sub.length() - 1;
    while(i < j) {
    if(sub[i++] != sub[j--]) return 0;
    }
    return 1;
    }
    };


Log in to reply
 

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