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:
3
Explanation:
All palindromic substrings are: "aba"
, "aa"
, and "baab"
.
Example 2
Input:
s = "aaa"
Output:
3
Explanation:
All palindromic substrings are: "aa"
, "aa"
, and "aaa"
.
Example 3
Input:
s = "abbaeae"
Output:
4
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] = true
and(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
, wheren
is the length ofs
.Traverse the string in reverse order for the starting index
i
(fromn-1
down to0
).Set
dp[i][i] = true
for alli
(single-character palindromes, though not counted, are needed to build multi-character palindromes).For each
i
, iteratej
fromi+1
ton-1
:If
s[i] == s[j]
and the substring in-between is a palindrome (orj - i == 1
for 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++)
class Solution {
public:
int countPS(string& s) {
int n = s.size(), res = 0;
vector<vector<bool>> dp(n, vector<bool>(n));
for (int i = n - 1; i >= 0; --i) {
dp[i][i] = true;
for (int j = i + 1; j < n; ++j)
if (s[i] == s[j] && (j - i == 1 || dp[i + 1][j - 1]))
dp[i][j] = true, res++;
}
return res;
}
};
Code (Java)
class Solution {
public int countPS(String s) {
int n = s.length(), res = 0;
boolean[][] dp = new boolean[n][n];
for (int i = n - 1; i >= 0; i--) {
dp[i][i] = true;
for (int j = i + 1; j < n; j++)
if (s.charAt(i) == s.charAt(j) && (j - i == 1 || dp[i + 1][j - 1])) {
dp[i][j] = true;
res++;
}
}
return res;
}
}
Code (Python)
class Solution:
def countPS(self, s):
n, res = len(s), 0
dp = [[False] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
dp[i][i] = True
for j in range(i + 1, n):
if s[i] == s[j] and (j - i == 1 or dp[i + 1][j - 1]):
dp[i][j] = True
res += 1
return res
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