647. Palindromic Substrings (Medium)

https://leetcode.com/problems/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.

 

Solutions

class Solution {

    public int countSubstrings(String s) {
        Set<String> validPalindromes = new HashSet<>();
        Set<String> invalidPalindromes = new HashSet<>();

        int ans = 0;

        if (s == null || s.isEmpty()) {
            return ans;
        }

        int len = s.length();

        // i is palindrome length
        for (int i = 1; i <= len; i++) {

            // possible palindrome of length i starts from j
            for (int j = 0; j < len - i + 1; j++) {
                String subStr = s.substring(j, j + i);

                if (invalidPalindromes.contains(subStr)) {
                    continue;
                }

                if (validPalindromes.contains(subStr)) {
                    ans++;

                    continue;
                }

                // of length 1, all the single chars are palindrome
                if (i == 1) {
                    ans++;

                    validPalindromes.add(subStr);

                    continue;
                }

                // of length 2
                if (i == 2) {
                    // if first chart == second char
                    if (subStr.charAt(0) == subStr.charAt(1)) {

                        ans++;

                        validPalindromes.add(subStr);
                    } else {
                        invalidPalindromes.add(subStr);
                    }

                    // others with different chars are not palindrome
                    continue;
                }

                // for length >= 2, expend to both side by 1 char

                // FIXME Following mistake should be avoid.
                // ignore cases that overstep the boundary
                // if (j == 0 || j + i == len) {
                //     continue;
                // }

                if (!validPalindromes.contains(subStr.substring(1, subStr.length() - 1))) {
                    continue;
                }

                if (subStr.charAt(0) != subStr.charAt(subStr.length() - 1)) {
                    invalidPalindromes.add(subStr);
                } else {
                    ans++;

                    validPalindromes.add(subStr);
                }
            }
        }

        return ans;
    }
}

Incorrect Solutions

References

Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-12-03 11:01:18

results matching ""

    No results matching ""