Minimum Insertion Steps to Make a String Palindrome

Minimum Insertion Steps to Make a String Palindrome is a Hard Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 2D Array (Longest Common Subsequence - LCS) subpattern.

Problem statement

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 palindrome.

A Palindrome String is one that reads the same backward as well as forward.

Example 1: Input: s = "zzazz" Output: 0 Explanation: The string "zzazz" is already palindrome we don't need any insertions.

Example 2: Input: s = "mbadm" Output: 2 Explanation: String can be "mbdadbm" or "mdbabdm".

Example 3: Input: s = "leetcode" Output: 5 Explanation: Inserting 5 characters the string becomes "leetcodocteel".

Example 4: Input: s = "g" Output: 0

Example 5: Input: s = "no" Output: 1

Constraints: 1 <= s.length <= 500 All characters of s are lower case English letters.

Topics and companies

Topics: Dynamic Programming.

Reported in interviews at Amazon, LinkedIn.

How to practise Minimum Insertion Steps to Make a String Palindrome on Thita.ai

Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n^2) time and O(n) space.

Open Minimum Insertion Steps to Make a String Palindrome in the code editor, or read the Minimum Insertion Steps to Make a String Palindrome editorial for a worked solution with its approach and complexity analysis.

Where Minimum Insertion Steps to Make a String Palindrome sits in the DSA pattern sheet

Problems related to Minimum Insertion Steps to Make a String Palindrome

Other problems that use the same 2D Array (Longest Common Subsequence - LCS) and Dynamic Programming (DP) Patterns techniques:

Opening the problem

Progress is saved as you study.