Similar Problems
Similar Problems not available
Maximum Number Of Non Overlapping Palindrome Substrings - Leetcode Solution
Companies:
LeetCode: Maximum Number Of Non Overlapping Palindrome Substrings Leetcode Solution
Difficulty: Hard
Topics: string dynamic-programming
Problem Statement:
Given a string s, find the maximum number of non-overlapping palindromic substrings of s.
Example:
Input: "ababcbacadefegdehijhklij" Output: 7 Explanation: The optimal way to split this string is "ababa", "c", "a", "defegde", "hijh", "kl", "ij", hence a total of 7 substrings.
Solution:
We can solve this problem using dynamic programming. Let's define dp[i] as the maximum number of non-overlapping palindromic substrings ending at index i in the given string. To calculate dp[i], we can try to extend all the previously calculated palindromes.
For each index i, we can consider two cases:
-
We can extend a palindrome that ends at i - 1 to include i. If s[j:i] is a palindrome (where j = i - length + 1 and length is the length of the palindrome ending at i - 1), we can extend this palindrome to include i. In this case, dp[i] will be equal to dp[j-1] + 1.
-
We can consider a palindrome ending at i that does not overlap with any palindrome ending before i. In this case, dp[i] will be equal to dp[i-1] + 1.
We need to take the maximum of these two cases and store it in dp[i].
At the end, dp[n-1] will give us the maximum number of non-overlapping palindromic substrings in the given string.
For the implementation, we need to find all the palindromes ending at index i. We can do this by expanding around the center of the palindrome.
Here is the Python code for the solution:
def countSubstrings(s: str) -> int: n = len(s) dp = [0] * n
for i in range(n):
# Case 2: consider palindrome ending at i that does not overlap with any palindrome ending before i
dp[i] = dp[i-1] + 1
# Case 1: try to extend all previously calculated palindromes
for j in range(i):
length = i - j + 1
if s[j:i+1] == s[j:i+1][::-1] and (length <= 2 or dp[j-1] != 0):
dp[i] = max(dp[i], dp[j-1] + 1)
return dp[n-1]
Time Complexity: O(n^2) where n is the length of the given string.
Space Complexity: O(n) as we are storing the dp array of size n.
Maximum Number Of Non Overlapping Palindrome Substrings Solution Code
1One possible solution is to use a brute force approach, where we check every substring to see if it is a palindrome. If it is, we add it to our list of palindromes. We can then return the length of this list.
2
3int findMaxNumOfPalindromes(string s) {
4 int n = s.length();
5 vector<string> palindromes;
6 // check every substring
7 for (int i = 0; i < n; i++) {
8 for (int j = i+1; j <= n; j++) {
9 string sub = s.substr(i, j-i);
10 // check if substring is a palindrome
11 if (isPalindrome(sub)) {
12 palindromes.push_back(sub);
13 }
14 }
15 }
16 return palindromes.size();
17}
18
19// helper function to check if a string is a palindrome
20bool isPalindrome(string s) {
21 int n = s.length();
22 for (int i = 0; i < n/2; i++) {
23 if (s[i] != s[n-1-i]) {
24 return false;
25 }
26 }
27 return true;
28}