LeetCode 647: Palindromic Substrings Solution

Master LeetCode problem 647 (Palindromic Substrings), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

647. Palindromic Substrings

Problem Explanation

Explanation

To solve this problem, we can iterate through each character in the string s and treat it as the center of a palindrome. We expand outwards from each center to check for palindromic substrings. There are two cases to consider:

  1. Palindromes with single center (e.g., "aba")
  2. Palindromes with double center (e.g., "abba")

By considering both cases, we can count all palindromic substrings in the given string.

Algorithm:

  1. Initialize a count variable to store the total count of palindromic substrings.
  2. Iterate through each character in the string s: a. For each character, consider it as the center of a palindrome and expand outwards to count palindromes with a single center. b. Also, consider this character and its right neighbor as the double center of a palindrome and expand outwards to count palindromes with a double center.
  3. Return the total count of palindromic substrings.

Time Complexity:

The time complexity of this algorithm is O(n^2), where n is the length of the input string s. This is because we expand around each character in the string to find palindromic substrings.

Space Complexity:

The space complexity of this algorithm is O(1) as we are not using any extra space proportional to the input size.

Solution Code

class Solution {
    public int countSubstrings(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            count += countPalindromes(s, i, i); // single center palindromes
            count += countPalindromes(s, i, i + 1); // double center palindromes
        }
        return count;
    }
    
    private int countPalindromes(String s, int left, int right) {
        int count = 0;
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            count++;
            left--;
            right++;
        }
        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 647 (Palindromic Substrings)?

This page provides optimized solutions for LeetCode problem 647 (Palindromic Substrings) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 647 (Palindromic Substrings)?

The time complexity for LeetCode 647 (Palindromic Substrings) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 647 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 647 in Java, C++, or Python.

Back to LeetCode Solutions