githubEdit

27(July) Form a palindrome

27. Form a Palindrome

The problem can be found at the following link: Question Linkarrow-up-right

Problem Description

Given a string, find the minimum number of characters to be inserted to convert it to a palindrome.

Examples:

Input:

str = "abcd"

Output:

3

Explanation: Inserted characters marked with bold characters in dcbabcd, here we need minimum three characters to make it palindrome.

My Approach

  1. Initialization:

    • Create a 2D table table of size n x n where n is the length of the string.

    • Initialize the table with 0, where table[i][j] will store the minimum number of insertions needed to convert the substring str[i...j] into a palindrome.

  2. Filling the Table:

    • Iterate over the gap gap from 1 to n-1.

    • For each gap, iterate over the substring starting index l from 0 to n-gap-1, and calculate the ending index h = l + gap.

    • If the characters at str[l] and str[h] are the same, then table[l][h] = table[l+1][h-1].

    • Otherwise, table[l][h] = min(table[l][h-1], table[l+1][h]) + 1.

  3. Return:

    • The value at table[0][n-1] will be the minimum number of insertions needed to convert the entire string into a palindrome.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n^2), as we fill an n x n table where each cell computation takes constant time.

  • Expected Auxiliary Space Complexity: O(n^2), as we use a 2D table of size n x n to store the results of subproblems.

Code (C++)

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questionsarrow-up-right. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated