Minimum insertions to make string palindrome

Dynamic Programming DP on strings Hard
  • Fun Fact: The underlying concept for this problem is extensively used in bioinformatics, especially in the development of various genome sequence alignment algorithms
  • In the context of the software industry, methods to examine and create palindromes are heavily used in data validation, error checking, and text analysis tools
  • Additionally, these concepts are used to create all kinds of puzzles and games that enhance mental skills and are found in many educational apps

Given a string s, find the minimum number of insertions needed to make it a palindrome. A palindrome is a sequence that reads the same backward as forward. You can insert characters at any position in the string.

Examples:

Input: s = "abcaa"


Output: 2


Explanation: Insert 2 characters "c", and "b" to make "abcacba", which is a palindrome.

Input: s = "ba"


Output: 1


Explanation: Insert "a" at the beginning to make "aba", which is a palindrome.

Input: s = "madam"

Constraints

  • 1 <= s.length <= 1000,
  • s consists of only lowercase English letters

Hints

  • "The minimum insertions required to make s a palindrome is the number of characters not already part of the longest palindromic subsequence (LPS). Min Insertions=Length of s−Length of LPS(s)"
  • "Compute LCS(s, reverse(s)) using Dynamic Programming (DP). The LCS of s and reverse(s) gives the LPS of s because a palindrome reads the same forward and backward."

Company Tags

Boston Consulting Group Deloitte Western Digital Rakuten Red Hat Epic Systems Seagate Technology Splunk Oracle Target Freshworks NVIDIA Square Rockstar Games PayPal Wayfair Philips Healthcare Riot Games Walmart Morgan Stanley Swiggy IBM Mastercard Snowflake Goldman Sachs Google Microsoft Amazon Meta Apple Netflix Adobe