Skip to content

Latest commit

 

History

History

647.Palindromic-Substrings

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

647. Palindromic Substrings

Untitled

  • traverse the string, use index as the center of the palindrom
class Solution {
    public int countSubstrings(String s) {
        int len = s.length();
        if(len == 0 || s == null) return 0;
        int count = 0;
        
        for(int i = 0; i < len; i++) {
            count += findPalindrom(i, i, s);
            count += findPalindrom(i, i + 1, s);
        }
        
        return count;
    }
    
    private int findPalindrom(int left, int right, String s) {
        int count = 0;
        int len = s.length();
        while(left >= 0 && right < len && s.charAt(left) == s.charAt(right) ) {
            left --;
            right ++;
            count ++;
        }
        
        return count;
    }
}