Minimum Insertion Steps to Make a String Palindrome
Updated
Problem statement
You are given a string s. In one step you may insert any single character at any position of the string (at the start, at the end, or between two characters).
Return the smallest number of steps needed to make s a palindrome. A palindrome reads the same forwards and backwards.
Function
minInsertions(s: String) → intExamples
Example 1
s = "abca"return = 1Insert b between c and the last a to get abcba, which is a palindrome. abca itself is not a palindrome, so at least one step is needed.
Example 2
s = "race"return = 3Insert e, c, and a at the front to get ecarace. No two letters of race are equal, so every letter except one must get a matching partner, and each partner costs one step.
Example 3
s = "noon"return = 0noon is already a palindrome, so no steps are needed.
Constraints
1 <= s.length <= 500scontains only lowercase English letters
More Infosys Onsite Interview questions
- Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
- Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Maximize the Even-Odd Index Product GapONSITE INTERVIEW · Seen Oct 2026
- Minimum Nonadjacent Flips for an Alternating Binary StringONSITE INTERVIEW · Seen Oct 2026
- Tenth-Highest Distinct SalaryONSITE INTERVIEW · Seen Oct 2026
- Assign Cookies With Matching ParityONSITE INTERVIEW · Seen Sep 2026
- Count Equal Code Pairs Within Distance KONSITE INTERVIEW · Seen Sep 2026
- Daily Successful Transactions Per DepartmentONSITE INTERVIEW · Seen Sep 2026
- Detect Substring In Any RotationONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Wildcard MatchingTripjack · ONSITE INTERVIEWTripjack interview questions
- Concatenated WordsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Interleaving StringsCARS24 · ONSITE INTERVIEW
- Minimum Stickers to Form a Target StringByteDance · ONSITE INTERVIEWByteDance interview questions
- Longest Valid ParenthesesMathWorks · ONSITE INTERVIEWMathWorks interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Word Break II: All SentencesMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Longest Valid ParenthesesMaven Clinic · ONSITE INTERVIEWMaven Clinic interview questions
- Word Break IIGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Bouquet ValueOracle · ONSITE INTERVIEWOracle interview questions