1. Recursion

class Solution:
    def maxPalindromes(self, s: str, k: int) -> int:
        n = len(s)

        def isPalindrome(l, r):
            while l < r:
                if s[l] != s[r]:
                    return False
                l += 1
                r -= 1
            return True

        def dfs(i):
            if i == n:
                return 0

            res = dfs(i + 1)
            for j in range(i + k - 1, n):
                if isPalindrome(i, j):
                    res = max(res, 1 + dfs(j + 1))
            return res

        return dfs(0)

Time & Space Complexity

  • Time complexity: O(n∗2n)O(n * 2 ^ n)
  • Space complexity: O(n)O(n) for the recursion stack.

Where nn is the length of the string ss.


2. Dynamic Programming (Top-Down) - I

class Solution:
    def maxPalindromes(self, s: str, k: int) -> int:
        n = len(s)
        dp = [-1] * n

        def isPalindrome(l, r):
            while l < r:
                if s[l] != s[r]:
                    return False
                l += 1
                r -= 1
            return True

        def dfs(i):
            if i == n:
                return 0
            if dp[i] != -1:
                return dp[i]

            res = dfs(i + 1)
            for j in range(i + k - 1, n):
                if isPalindrome(i, j):
                    res = max(res, 1 + dfs(j + 1))
            dp[i] = res
            return res

        return dfs(0)

Time & Space Complexity

  • Time complexity: O(n3)O(n ^ 3)
  • Space complexity: O(n)O(n)

Where nn is the length of the string ss.


3. Dynamic Programming (Top-Down) - II

class Solution:
    def maxPalindromes(self, s: str, k: int) -> int:
        n = len(s)
        isPal = [[None] * n for _ in range(n)]
        dp = [-1] * n

        def isPalindrome(l, r):
            if l >= r:
                return True
            if isPal[l][r] is not None:
                return isPal[l][r]
            isPal[l][r] = s[l] == s[r] and isPalindrome(l + 1, r - 1)
            return isPal[l][r]

        def dfs(i):
            if i == n:
                return 0
            if dp[i] != -1:
                return dp[i]

            res = dfs(i + 1)
            for j in range(i + k - 1, n):
                if isPalindrome(i, j):
                    res = max(res, 1 + dfs(j + 1))
            dp[i] = res
            return res

        return dfs(0)

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity: O(n2)O(n ^ 2)

Where nn is the length of the string ss.


4. Dynamic Programming (Bottom-Up)

class Solution:
    def maxPalindromes(self, s: str, k: int) -> int:
        n = len(s)
        isPal = [[False] * n for _ in range(n)]
        for i in range(n - 1, -1, -1):
            for j in range(i, n):
                if s[i] == s[j] and (j - i < 2 or isPal[i + 1][j - 1]):
                    isPal[i][j] = True

        dp = [0] * (n + 1)
        for i in range(n - 1, -1, -1):
            dp[i] = dp[i + 1]
            for j in range(i + k - 1, n):
                if isPal[i][j]:
                    dp[i] = max(dp[i], 1 + dp[j + 1])

        return dp[0]

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity: O(n2)O(n ^ 2)

Where nn is the length of the string ss.


5. Expand Around Center

class Solution:
    def maxPalindromes(self, s: str, k: int) -> int:
        n = len(s)
        dp = [0] * (n + 1)

        def expand(l, r):
            while l >= 0 and r < n and s[l] == s[r]:
                if r - l + 1 >= k:
                    dp[r + 1] = max(dp[r + 1], dp[l] + 1)
                    break
                l -= 1
                r += 1

        for mid in range(n):
            dp[mid + 1] = max(dp[mid + 1], dp[mid])
            expand(mid, mid)
            expand(mid - 1, mid)

        return dp[n]

Time & Space Complexity

  • Time complexity: O(n∗k)O(n * k)
  • Space complexity: O(n)O(n)

Where nn is the length of the string ss and kk is the minimum length of a selected palindrome.


6. Greedy

class Solution:
    def maxPalindromes(self, s: str, k: int) -> int:
        n = len(s)

        def isPalindrome(l, r):
            while l < r:
                if s[l] != s[r]:
                    return False
                l += 1
                r -= 1
            return True

        res = 0
        start = 0
        for i in range(k - 1, n):
            if i - k + 1 >= start and isPalindrome(i - k + 1, i):
                res += 1
                start = i + 1
            elif i - k >= start and isPalindrome(i - k, i):
                res += 1
                start = i + 1

        return res

Time & Space Complexity

  • Time complexity: O(n∗k)O(n * k)
  • Space complexity: O(1)O(1) extra space.

Where nn is the length of the string ss and kk is the minimum length of a selected palindrome.