严氏北美IT公司面试真题汇总和解答论坛
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Palindromic Substrings

Go down

Palindromic Substrings Empty Palindromic Substrings

Post by Admin Sat Oct 21, 2017 2:29 pm

static int count = 0;
   
   public static int countSubstrings(String s) {
       if (s == null || s.length() == 0) return 0;
       
       for (int i = 0; i < s.length(); i++) { // i is the mid point
           extendPalindrome(s, i, i); // odd length;
           extendPalindrome(s, i, i + 1); // even length
       }
       
       return count;
   }
   
   private static void extendPalindrome(String s, int left, int right) {
       while (left >=0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
           count++; left--; right++;
       }
   }
   
public static void main(String[] args) {
// TODO Auto-generated method stub
        int res=countSubstrings("aaa");
        System.out.println(res);
}

Admin
Admin

Posts : 124
Join date : 2017-10-21

https://csinterviewquestions.forumotion.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum