10. Palindrome SubStrings
β GFG solution to the Palindrome SubStrings problem: count all palindromic substrings of length β₯ 2 using efficient center expansion technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a string s. Your task is to count all palindromic sub-strings present in the string where the length of the palindromic sub-string must be greater than or equal to 2.
A substring is palindromic if it reads the same forwards and backwards. The goal is to efficiently count all such valid palindromic substrings.
π Examples
Example 1
Input: s = "abaab"
Output: 3
Explanation: All palindromic substrings (of length > 1) are: "aba", "aa", "baab".Example 2
Input: s = "aaa"
Output: 3
Explanation: All palindromic substrings (of length > 1) are: "aa", "aa", "aaa".Example 3
π Constraints
$2 \le s.size() \le 5 \times 10^3$
$s$ contains only lowercase english characters
β
My Approach
The optimal approach uses the Center Expansion technique to efficiently find all palindromic substrings:
Center Expansion Technique
Core Idea:
For each possible center position, expand outwards to find all palindromes.
Handle both odd-length (single center) and even-length (two adjacent centers) palindromes.
Algorithm Steps:
For each index
ifrom 0 to n-1:Odd-length palindromes: Expand around center
iwith left=i-1, right=i+1Even-length palindromes: Expand around centers
iandi+1with left=i, right=i+1
Continue expansion while characters match and boundaries are valid.
Count each valid palindrome found during expansion.
Expansion Process:
Start from potential center(s) and expand outwards.
While
left >= 0,right < n, ands[left] == s[right]:Increment count (valid palindrome found).
Move
left--andright++to check longer palindromes.
Why This Works:
Every palindrome has a center (either single character or between two characters).
By checking all possible centers, we capture all palindromes.
Expansion ensures we find palindromes of all lengths from each center.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(nΒ²), where n is the length of the string. In the worst case (all characters same), each center can expand to create O(n) palindromes, and we have O(n) centers.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables like pointers and counters, regardless of input size.
π§βπ» Code (C++)
β‘ View Alternative Approaches with Code and Analysis
π 2οΈβ£ Dynamic Programming Approach
π‘ Algorithm Steps:
Create a 2D DP table where dp[i][j] represents if substring from i to j is palindrome.
Fill two-character substrings first (skip single characters as we want length > 1).
For longer substrings, check if first and last characters match and inner substring is palindrome.
Count only palindromic substrings with length greater than 1.
π Complexity Analysis:
Time: β±οΈ O(nΒ²) - Fill DP table for all substrings
Auxiliary Space: πΎ O(nΒ²) - 2D DP table storage
β
Why This Approach?
Systematic bottom-up approach
Can be extended to find longest palindromic substring
Clear state transitions and memoization
π 3οΈβ£ Manacher's Algorithm
π‘ Algorithm Steps:
Transform string by inserting '#' between characters to handle even/odd length uniformly.
Use previously computed palindrome information to avoid redundant checks.
Maintain rightmost boundary of discovered palindromes for optimization.
Count palindromes efficiently using symmetry properties.
π Complexity Analysis:
Time: β±οΈ O(n) - Linear time processing
Auxiliary Space: πΎ O(n) - Transformed string and palindrome array
β
Why This Approach?
Optimal linear time complexity
Handles both even and odd length palindromes uniformly
Advanced algorithm for competitive programming
π 4οΈβ£ Brute Force with Optimizations
π‘ Algorithm Steps:
Check every possible substring for palindrome property.
Use early termination when characters don't match.
Optimize by checking from both ends simultaneously.
Skip obviously non-palindromic cases early.
π Complexity Analysis:
Time: β±οΈ O(nΒ³) - Three nested loops
Auxiliary Space: πΎ O(1) - Constant space usage
β
Why This Approach?
Simple and intuitive implementation
Easy to understand and debug
Good for small input sizes
Note: This approach results in Time Limit Exceeded (TLE) for large inputs (fails ~1110 /1120 test cases due to time constraints).
π π Comparison of Approaches
π Approach
β±οΈ Time Complexity
πΎ Space Complexity
β Pros
β οΈ Cons
π·οΈ Center Expansion
π’ O(nΒ²)
π’ O(1)
π Optimal space, intuitive
π§ Careful center handling needed
π Dynamic Programming
π’ O(nΒ²)
π‘ O(nΒ²)
π Clear state transitions
πΎ Extra space for DP table
π Manacher's Algorithm
π’ O(n)
π‘ O(n)
π― Linear time complexity
π Complex implementation logic
π Brute Force
π‘ O(nΒ³)
π’ O(1)
β Simple to implement
π§ Inefficient for large inputs
π Best Choice Recommendation
π― Scenario
ποΈ Recommended Approach
π₯ Performance Rating
π Optimal time-space balance
π₯ Center Expansion
β β β β β
π Need to find actual palindromes
π₯ Dynamic Programming
β β β β β
π§ Large inputs, need linear time
π₯ Manacher's Algorithm
β β β β β
π― Learning/Simple implementation
π Brute Force
β β β ββ
β Code (Java)
π Code (Python)
π§ 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