From a9fafb1296178dfdc0a9ff44ebdd56b02399d4ea Mon Sep 17 00:00:00 2001 From: Yatin Aggarwal Date: Mon, 7 Jul 2025 00:09:32 +0530 Subject: [PATCH] Create 1312-Minimum-Insertion-Steps-to-Make-a-String-Palindrome.py --- ...-Insertion-Steps-to-Make-a-String-Palindrome.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 python/1312-Minimum-Insertion-Steps-to-Make-a-String-Palindrome.py diff --git a/python/1312-Minimum-Insertion-Steps-to-Make-a-String-Palindrome.py b/python/1312-Minimum-Insertion-Steps-to-Make-a-String-Palindrome.py new file mode 100644 index 000000000..3c2206d38 --- /dev/null +++ b/python/1312-Minimum-Insertion-Steps-to-Make-a-String-Palindrome.py @@ -0,0 +1,14 @@ +def ans(s , i , j ,dp): + if(i > j ): + return 0 + if(dp[i][j]!= -1): + return dp[i][j] + if(s[i]== s[j]): + dp[i][j] = ans(s, i+1, j-1,dp) + return dp[i][j] + dp[i][j]= 1+min(ans(s, i+1,j,dp), ans(s, i, j-1,dp)) + return dp[i][j] +class Solution(object): + def minInsertions(self, s): + dp = [[-1]*len(s) for i in range(len(s))] + return ans(s, 0 , len(s)-1, dp)