Minimum Insertion Steps to Make a String Palindrome
HardDynamic Programminginterval dp
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given a string s, you may insert any character at any position, one insertion per step. Return the minimum number of insertions needed to turn s into a palindrome.
A palindrome reads the same forwards and backwards. A string that is already a palindrome needs 0 steps. An O(n^2) time solution is expected.
Examples
Input: s = "zzazz"
Output: 0
The string is already a palindrome.
Input: s = "mbadm"
Output: 2
Insert two characters to get "mbdadbm" (or "mdbabdm").
Input: s = "leetcode"
Output: 5
Five insertions suffice, e.g. "leetcodocteel", and no smaller number works.
Constraints
- 1 <= s.length <= 500
- s consists of lowercase English letters
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.