9. Palindrome SubStrings
The problem can be found at the following link: Question Link
Problem Description
Given a string s, count all palindromic substrings present in the string where the length of each palindromic substring is greater than or equal to 2.
Examples
Example 1
Input:
s = "abaab"Output:
3Explanation:
All palindromic substrings are: "aba", "aa", and "baab".
Example 2
Input:
s = "aaa"Output:
Explanation:
All palindromic substrings are: "aa", "aa", and "aaa".
Example 3
Input:
Output:
Explanation:
All palindromic substrings are: "bb", "abba", "aea", and "eae".
Constraints
$( 2 \leq \text{length}(s) \leq 10^3 )$
String contains only lowercase English characters.
My Approach
Dynamic Programming (DP) (O(N²) Time, O(N²) Space)
We use a 2D boolean DP array
dp[i][j]to indicate whether the substrings[i..j]is a palindrome.Base cases: Every single character is trivially a palindrome (we won't count single-character palindromes as per problem statement).
We fill the DP table in a manner that checks if the characters at both ends match and if the inside substring is also a palindrome.
Whenever we find
dp[i][j] = trueand(j - i + 1) >= 2, we increment our palindrome count.Return the final count of palindromic substrings of length >= 2.
Algorithm Steps
Create a 2D array
dp[n][n], initialized tofalse, wherenis the length ofs.Traverse the string in reverse order for the starting index
i(fromn-1down to0).Set
dp[i][i] = truefor alli(single-character palindromes, though not counted, are needed to build multi-character palindromes).For each
i, iteratejfromi+1ton-1:If
s[i] == s[j]and the substring in-between is a palindrome (orj - i == 1for adjacent chars), setdp[i][j] = true.If
dp[i][j]istrue, increment the count.
The result is the total count of palindromic substrings of length ≥ 2.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(N²), where N is the length of the string. We have nested loops iterating through the string to fill the DP table.
Expected Auxiliary Space Complexity: O(N²), for maintaining the 2D DP table.
Code (C++)
⚡ Alternative Approaches
1️⃣ Expand Around Center (O(N²) Time, O(1) Space)
Idea:
Treat each index (and index gap) as a potential palindrome center.
Expand outward while the characters match.
Count palindromic substrings of length ≥ 2.
🔹 No extra space needed 🔹 Simple to implement
Code (Java)
Code (Python)
2️⃣ Manacher’s Algorithm (O(N) Time, O(N) Space)
Idea:
Transform string into a format with separators (e.g.,
#a#b#a#b#).Use Manacher’s algorithm to find palindromes in
O(N).Count palindromes of length ≥ 2 from the computed radius array.
🔹 Fastest for large N
🔹 Trickier to implement
📊 Comparison of Approaches
Approach
⏱️ Time Complexity
🗂️ Space Complexity
✅ Pros
⚠️ Cons
Dynamic Programming (DP)
🟡 O(N²)
🟡 O(N²)
Reliable and efficient for mid-size N
Requires 2D DP table
Expand Around Center
🟡 O(N²)
🟢 O(1)
Simple and space-efficient
Slower for very large N
Manacher’s Algorithm
🟢 O(N)
🟡 O(N)
Fastest for N > 10⁵
Complex to implement
💡 Best Choice?
✅ For simplicity: Use Expand Around Center.
✅ For optimal runtime on large inputs: Use Manacher’s Algorithm.
✅ For learning DP concepts: Use the Dynamic Programming approach.
Code (Java)
Code (Python)
Note: Single-character palindromes are not counted as per problem statement. All approaches shown ensure only palindromes of length ≥ 2 are incremented.
Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated