其他分享
首页 > 其他分享> > 647. Palindromic Substrings

647. Palindromic Substrings

作者:互联网

This is a "palindromic" problem, I think DP can solve this problem, but there is a easier way to solve it, the time complexity is O(n2):

    private int res = 0;
    public int countSubstrings(String s) {
        for(int i=0;i<s.length();i++){
            helper(s, i, i);    // count the palindromic with odd number, eg. eabae
            helper(s, i, i+1);  // count the palindromic with even number, eg. abba
        }
        return res;
    }
    
    private void helper(String s, int l, int r){
        while(l>=0 && r<s.length()){
            if(s.charAt(l)==s.charAt(r)){
                res++;
                l--;
                r++;
            }
            else
                return;
        }
    }

 

标签:Palindromic,int,private,Substrings,solve,647,problem,countSubstrings,DP
来源: https://www.cnblogs.com/feiflytech/p/15861249.html