Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Insert Minimum To Make Palindrome Editorial

DSA Editorial, Solution and Code

Practice Problem Link: https://workat.tech/problem-solving/practice/min-insertion-palindrome

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string s, find the minimum number of characters you need to insert at the beginning in order to make the string a palindrome.

Naive Approach

The simple solution is to find the maximum palindromic string starting from the index 0. The answer will be 

n - length of maximum palindromic string since the remaining elements after the palindromic string must be added before the palindromic string to make the string s a palindrome.

  • Run two nested loops, the outer loop from last = n to last = 1 and the inner loop from 0 to last to check if the current substring is palindromic.
  • As soon as a palindromic string is found return n - last as answer.

Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
int minCharactersToBeInserted (string A) {
    int n = A.size();
	for (int last = n; last > 0; last--) {
		int isPalindrome = 1;
		for (int i = 0; i < last / 2; i++) {
			if (A[i] != A[last - i - 1]) {
				isPalindrome = 0;
				break;
			}
		}
		if (isPalindrome) {
			return n - last;
		}
	}
}
Java
class Solution {
	int minCharactersToBeInserted (String A) {
	    int n = A.length();
		for (int last = n; last > 0; last--) {
			int isPalindrome = 1;
			for (int i = 0; i < last / 2; i++) {
				if (A.charAt(i) != A.charAt(last - i - 1)) {
					isPalindrome = 0;
					break;
				}
			}
			if (isPalindrome == 1) {
				return n - last;
			}
		}
		return 0;
	}
}

Optimal Approach

The problem can be solved more efficiently by using the concept of LPS which is used in the Knuth Morris Pratt (KMP) Algorithm. LPS of a string is the longest proper prefix which is also a suffix.

Example:

String: AABCDAA

Proper prefixes: A,AA,AAB,AABC,AABCD and AABCDA.

LPS of the above-given string is AA.

To solve this problem we will create an lps array where lps[i] denotes the length of the lps of substring s[0…i].

  • Here, the idea is to reverse the given string and append it in the original string separated by a special character(say ‘#’) to find the maximum palindromic string.
  • Now traverse the string and find the lps array of the string.
  • Only the last element of the lps array is useful to us as it shows the largest suffix of the reversed string that is equal to the prefix of the original string (i.e it gives us the maximum length substring which satisfies the palindrome property).
  • The number of elements required to be added to make the string palindrome will be the length of the original array minus the value of the last element of the lps array.

Example:

String S = ACBABCADE

S after appending its reverse = ACBABCADE#EDACBABCA

LPS array of the string = {0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7}

Answer = n - last element of lps array = 9 - 7 = 2

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(n)

Implementation

C++
 int minCharactersToBeInserted (string s) {
	string rev = s;
	reverse (rev.begin(), rev.end());
	s += '#';
	s += rev;
	int n = s.size(), lps[n], palindromeLength = 0;
	lps[0] = 0;
	int i = 1;
	while(i < n) {
		if (s[i] == s[palindromeLength]) {
			palindromeLength++;
			lps[i++] = palindromeLength;
		} else {
			if (palindromeLength == 0)
				lps[i++] = 0;
			else
				palindromeLength = lps[palindromeLength - 1];
		}
	}
	int ans = n / 2 - lps[n - 1];
	return ans;
}
Java
class Solution {
	int minCharactersToBeInserted (String s) {
		StringBuilder rev = new StringBuilder();
		rev.append(s);
		rev.reverse();
		s += '#';
		s += rev;
		int n = s.length(), palindromeLength = 0;
		int[] lps = new int[n];
		lps[0] = 0;
		int i = 1;
		while(i < n) {
			if (s.charAt(i) == s.charAt(palindromeLength)) {
				palindromeLength++;
				lps[i++] = palindromeLength;
			} else {
				if (palindromeLength == 0)
					lps[i++] = 0;
				else
					palindromeLength = lps[palindromeLength - 1];
			}
		}
		int ans = n / 2 - lps[n - 1];
		return ans;
	}
}
Related Content
Anagrams
Compare Version Numbers
Count And Say
Integer to Roman Numeral
Longest Common Prefix
Longest Palindrome in String
Reverse Words in String
Roman Numeral to Integer
Substring Search - I
Substring Search - III
Substring Search - II
Substring Search - IV
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet