1312. Minimum Insertion Steps to Make a String Palindrome

Minimum Insertion Steps to Make a String Palindrome (LeetCode 1312) 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.

How to solve Minimum Insertion Steps to Make a String Palindrome

Minimum Insertion Steps to Make a String Palindrome is a Longest Common Subsequence problem, part of Dynamic Programming (DP) Patterns. Find longest common subsequence between two strings using 2D DP.

Work towards the solution in these steps:

  1. Consider how dynamic programming can help solve problems involving subsequences and string transformations efficiently.
  2. Think about how the problem of making a string a palindrome relates to finding a longest palindromic subsequence within it.
  3. Recall that the longest palindromic subsequence can be found using the longest common subsequence (LCS) approach between the string and its reverse.

The reference solution runs in O(n^2) time and O(n) space.

The Minimum Insertion Steps to Make a String Palindrome solution and editorial walks through the full approach with reference code.

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:

Minimum Insertion Steps to Make a String Palindrome FAQ

What is the LeetCode number of Minimum Insertion Steps to Make a String Palindrome?

Minimum Insertion Steps to Make a String Palindrome is LeetCode problem 1312, rated Hard.

What pattern is used to solve Minimum Insertion Steps to Make a String Palindrome?

Minimum Insertion Steps to Make a String Palindrome is a Longest Common Subsequence problem. It belongs to the Dynamic Programming (DP) Patterns pattern, in the 2D Array (Longest Common Subsequence - LCS) subpattern.

What is the time complexity of Minimum Insertion Steps to Make a String Palindrome?

The reference solution runs in O(n^2) time and O(n) space.

Which companies ask Minimum Insertion Steps to Make a String Palindrome?

Minimum Insertion Steps to Make a String Palindrome has been reported in interviews at Amazon, LinkedIn.

Preparing this workspace.