Minimum Insertions to Make Palindrome šŸŽÆ

beginner
22 min

Minimum Insertions to Make Palindrome šŸŽÆ

Welcome to the exciting world of Data Structures and Algorithms! Today, we'll dive into a fascinating problem called "Minimum Insertions to Make Palindrome".

What's a Palindrome? šŸ“

A palindrome is a word, phrase, number, or any sequence of characters that reads the same backward as forward. For example, "racecar", "level", "121", and "A man, a plan, a canal: Panama" are all palindromes.

Minimum Insertions to Make Palindrome šŸ’”

Given a string s, the problem is to find the minimum number of insertions required to make it a palindrome.

Let's break this down:

  1. Identify the Character Imbalance šŸ“

    • Count the occurrences of each character in the string.
    • If the count of a character is odd, it means we have an odd number of that character, and it causes the string to not be a palindrome.
    • Remember, for a string to be a palindrome, the number of occurrences of all characters must be even (or zero).
  2. Find the Characters to be Inserted šŸ’”

    • Identify the characters with odd counts.
    • To balance the string, we need to insert these characters at appropriate places so that their count becomes even.
  3. Insert and Check šŸŽÆ

    • Start inserting characters one by one at the beginning of the string, checking after each insertion if the string is now a palindrome.
    • The first insertion that makes the string a palindrome is the minimum number of insertions required.

Example šŸ’”

Let's consider the string abccba. Here's how we'd find the minimum number of insertions to make it a palindrome:

  1. Identify the Character Imbalance

    • 'a' - 1 occurrence
    • 'b' - 1 occurrence
    • 'c' - 3 occurrences
  2. Find the Characters to be Inserted

    • Since 'a' and 'b' occur only once, we need to insert one of them at the beginning of the string.
  3. Insert and Check

    • Insert 'a' at the beginning: aabccba -> Not a palindrome.
    • Insert 'b' at the beginning: babccba -> Not a palindrome.
    • Insert 'a' again at the beginning: aaabccba -> Not a palindrome.
    • Insert 'b' again at the beginning: bbabccba -> Not a palindrome.
    • Now, insert 'c' at the beginning: ccbabccba -> cbcbabccba (reversed) -> ccbabccbac -> Palindrome!

Python Solution šŸ’”

Here's a Python solution for the problem:

python
def min_insertions(s): char_count = {} for char in s: if char in char_count: char_count[char] += 1 else: char_count[char] = 1 min_insertions = 0 for char in char_count: if char_count[char] % 2 != 0: min_insertions += 1 for i in range(len(s)): temp_s = s[:i] + s[::-1][i:] if temp_s == s or (min_insertions > 0 and temp_s == s[::-1]): return min_insertions min_insertions += 1 temp_s = s[:i] + char + s[i:] + s[::-1][i:] return min_insertions # Test the function print(min_insertions("abccba")) # Output: 1

Quiz Time šŸŽÆ

Quick Quiz
Question 1 of 1

What is a palindrome?

Happy coding! Let's turn more strings into palindromes together! šŸŽÆ