Problem statement

https://binarysearch.com/problems/Make-a-Palindrome-by-Inserting-Characters/

Solution

Equal to Leetcode 1312. Minimum Insertion Steps to Make a String Palindrome.

Complexity

It is O(n^2) for time and space.

Code

class Solution:
    def solve(self, s):
        @lru_cache(None)
        def dp(i, j):
            if j <= i: return 0
            if s[i] == s[j]: return dp(i + 1, j - 1)
            return min(dp(i + 1, j), dp(i, j - 1)) + 1
        
        return dp(0, len(s) - 1)