1. Recursion

class Solution:
    def minCut(self, s: str) -> 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 = n
            for j in range(i, n):
                if isPalindrome(i, j):
                    res = min(res, 1 + dfs(j + 1))
            return res

        return dfs(0) - 1

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)

class Solution:
    def minCut(self, s: str) -> 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 = n
            for j in range(i, n):
                if isPalindrome(i, j):
                    res = min(res, 1 + dfs(j + 1))
            dp[i] = res
            return res

        return dfs(0) - 1

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.


3. Dynamic Programming (Bottom-Up)

class Solution:
    def minCut(self, s: str) -> 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] = n
            for j in range(i, n):
                if isPal[i][j]:
                    dp[i] = min(dp[i], 1 + dp[j + 1])

        return dp[0] - 1

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 Optimized)

class Solution:
    def minCut(self, s: str) -> int:
        n = len(s)
        isPal = [[False] * n for _ in range(n)]
        dp = [0] * (n + 1)

        for i in range(n - 1, -1, -1):
            dp[i] = n
            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[i] = min(dp[i], 1 + dp[j + 1])

        return dp[0] - 1

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 minCut(self, s: str) -> int:
        n = len(s)
        dp = [i for i in range(n + 1)]

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

        for mid in range(n):
            expand(mid, mid)
            expand(mid - 1, mid)

        return dp[n] - 1

Time & Space Complexity

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

Where nn is the length of the string ss.