1. 程式人生 > >LeetCode:647. Palindromic Substrings(找出字串中所有的迴文子串)

LeetCode:647. Palindromic Substrings(找出字串中所有的迴文子串)

      Given a string, your task is to count how many palindromic substrings in this string.

      The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

Example 1:

Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Note:
  1. The input string length won't exceed 1000.

方法1:這種演算法是比較笨的演算法,當然效率也肯定不會高 

package leetcode;

import org.junit.Test;

/**
 * @author zhangyu
 * @version V1.0
 * @ClassName: PalindromicSubstrings
 * @Description: TOTO
 * @date 2018/12/5 20:35
 **/


public class PalindromicSubstrings {
    @Test
    public void fun() {
       // String s = "abc";
        String s="aaa";
        int number = palindromicSubstrings(s);
        System.out.println(number);
    }

    private int palindromicSubstrings(String s) {
        int count=0;
        for (int i = 0; i < s.length(); i++) {
            for (int j = i + 1; j <= s.length(); j++) {
                String subString = s.substring(i, j);
                if(isPalindromic2(subString)){
                    count++;
                    System.out.println(subString);
                }
            }
        }
        return count;
    }

    public static boolean isPalindromic2(String subString) {
        int i = 0;
        int j = subString.length() - 1;
        while (i < j) {
            if (subString.charAt(i) != subString.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

時間複雜度:O(n^3)

空間複雜度:O(n)


方法2:比較簡便的方法

package leetcode;

import org.junit.Test;

/**
 * @author zhangyu
 * @version V1.0
 * @ClassName: PalindromicSubstrings
 * @Description: TOTO
 * @date 2018/12/5 20:35
 **/


public class PalindromicSubstrings2 {
    @Test
    public void fun() {
        // String s = "abc";
        String s = "aaa";
        int number = countSubstrings(s);
        System.out.println(number);
    }

    public int countSubstrings(String s) {
        int N = s.length(), ans = 0;
        for (int center = 0; center <= 2 * N - 1; ++center) {
            int left = center / 2;
            int right = left + center % 2;
            while (left >= 0 && right < N && s.charAt(left) == s.charAt(right)) {
                ans++;
                left--;
                right++;
            }
        }
        return ans;
    }
}

時間複雜度:O(n^2)

空間複雜度:O(1)


方法3:題目提供的方式

class Solution {
    public int countSubstrings(String s) {
        int len = s.length();
        if(len <= 1) return len;
        
        int ans = 0;
        for(int i = 0; i < len; i++) {
            ans += getPalin(s, i, i);
            ans += getPalin(s, i, i+1);
        }
        
        return ans;
    }
    
    public int getPalin(String s, int l, int r) {
        int cnt = 0;
        while(l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            l--;
            r++;
            cnt++;
        }
        
        return cnt;
    }
}

時間複雜度:O(n^2)

空間複雜度:O(1)