Minimum Insertion Steps to Make a String Palindrome

Given a string s. In one step you can insert any character at any index of the string. Return the minimum number of steps to make s a palindrome. A palindrome string is one that reads the same backward as well as forward.

30:00

Minimum Insertion Steps to Make a String Palindrome
hard
Free written solution
Topics
Companies

Given a string s. In one step you can insert any character at any index of the string. Return the minimum number of steps to make s a palindrome. A palindrome string is one that reads the same backward as well as forward.

Example 1:
Input: {"s":"mbadm"}
Output: 2
Constraints:
  • 1≤s.length≤5001 \leq s.\text{length} \leq 500

  • ss consists of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"mbadm"}

dp[i][j] = insertions needed to make s[i..j] a palindrome. Row i is where the substring starts, column j is where it ends, so only cells with j >= i mean anything.

s

m

0

b

1

a

2

d

3

m

4

dp[i][j] (row = start i, column = end j), filled one diagonal at a time

m0
b1
a2
d3
m4
m0
b1
a2
d3
m4
Variables
VariableValue
n5
DepthFunction Call
Stack empty
0/16