Skip to main content

Minimum number of perpend character needed to Make a string palindrome

Here given code implementation process.

/*
  Java program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
public class Palindrome
{
	// Check that prefix of string is part of palindrome or not
	public boolean isPrefixPalindrome(String text, int n)
	{
		int i = 0;
		int j = n;
		while (i < j)
		{
			if (text.charAt(i) != text.charAt(j))
			{
				// When not a palindrome
				return false;
			}
			i++;
			j--;
		}
		// When yes
		return true;
	}
	public void makePalindromeByPerpend(String text)
	{
		if (text.length() == 0)
		{
			return;
		}
		int count = -1;
		// Get the length of text
		int n = text.length();
		// Executing this loop through by size of text length
		for (int i = n - 1; i >= 0 && count == -1; --i)
		{
			if (text.charAt(0) == text.charAt(i) && 
                isPrefixPalindrome(text, i))
			{
				// When Prefix 0..i is palindrome
				count = (n - 1) - i;
			}
		}
		// Display given text
		System.out.println(" Given Text : " + text);
		System.out.println(" Need to Perpend " + count + " Characters");
	}
	public static void main(String[] args)
	{
		Palindrome task = new Palindrome();
		// Test Inputs
		// Case A
		// Text : ipid
		//  d ipi d  [Add d at beginning] 
		// dipid => palindrome
		// Result :1
		task.makePalindromeByPerpend("ipid");
		// Case B
		// Text : 123321892
		// 298 123321 892 [Add 298 at beginning]
		// 298123321892 => palindrome
		// Result : 3
		task.makePalindromeByPerpend("123321892");
		// Case C
		// Text : 121
		// 121 it's already palindrome
		// Result : 0
		task.makePalindromeByPerpend("121");
		// Case D
		// Text : 12356
		// 6532 1 2356 
		// Result : 4
		task.makePalindromeByPerpend("12356");
	}
}

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
  C++ program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
class Palindrome
{
	public:
		// Check that prefix of string is part of palindrome or not
		bool isPrefixPalindrome(string text, int n)
		{
			int i = 0;
			int j = n;
			while (i < j)
			{
				if (text[i] != text[j])
				{
					// When not a palindrome
					return false;
				}
				i++;
				j--;
			}
			// When yes
			return true;
		}
	void makePalindromeByPerpend(string text)
	{
		if (text.length() == 0)
		{
			return;
		}
		int count = -1;
		// Get the length of text
		int n = text.length();
		// Executing this loop through by size of text length
		for (int i = n - 1; i >= 0 && count == -1; --i)
		{
			if (text[0] == text[i] && 
                this->isPrefixPalindrome(text, i))
			{
				// When Prefix 0..i is palindrome
				count = (n - 1) - i;
			}
		}
		// Display given text
		cout << " Given Text : " 
            << text << endl;
		cout << " Need to Perpend " 
      		 << count 
             << " Characters" << endl;
	}
};
int main()
{
	Palindrome *task = new Palindrome();
	// Test Inputs
	// Case A
	// Text : ipid
	//  d ipi d  [Add d at beginning] 
	// dipid => palindrome
	// Result :1
	task->makePalindromeByPerpend("ipid");
	// Case B
	// Text : 123321892
	// 298 123321 892 [Add 298 at beginning]
	// 298123321892 => palindrome
	// Result : 3
	task->makePalindromeByPerpend("123321892");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task->makePalindromeByPerpend("121");
	// Case D
	// Text : 12356
	// 6532 1 2356 
	// Result : 4
	task->makePalindromeByPerpend("12356");
	return 0;
}

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
// Include namespace system
using System;
/*
  Csharp program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
public class Palindrome
{
	// Check that prefix of string is part of palindrome or not
	public Boolean isPrefixPalindrome(String text, int n)
	{
		int i = 0;
		int j = n;
		while (i < j)
		{
			if (text[i] != text[j])
			{
				// When not a palindrome
				return false;
			}
			i++;
			j--;
		}
		// When yes
		return true;
	}
	public void makePalindromeByPerpend(String text)
	{
		if (text.Length == 0)
		{
			return;
		}
		int count = -1;
		// Get the length of text
		int n = text.Length;
		// Executing this loop through by size of text length
		for (int i = n - 1; i >= 0 && count == -1; --i)
		{
			if (text[0] == text[i] && 
                this.isPrefixPalindrome(text, i))
			{
				// When Prefix 0..i is palindrome
				count = (n - 1) - i;
			}
		}
		// Display given text
		Console.WriteLine(" Given Text : " + text);
		Console.WriteLine(" Need to Perpend " + count + " Characters");
	}
	public static void Main(String[] args)
	{
		Palindrome task = new Palindrome();
		// Test Inputs
		// Case A
		// Text : ipid
		//  d ipi d  [Add d at beginning] 
		// dipid => palindrome
		// Result :1
		task.makePalindromeByPerpend("ipid");
		// Case B
		// Text : 123321892
		// 298 123321 892 [Add 298 at beginning]
		// 298123321892 => palindrome
		// Result : 3
		task.makePalindromeByPerpend("123321892");
		// Case C
		// Text : 121
		// 121 it's already palindrome
		// Result : 0
		task.makePalindromeByPerpend("121");
		// Case D
		// Text : 12356
		// 6532 1 2356 
		// Result : 4
		task.makePalindromeByPerpend("12356");
	}
}

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
package main
import "fmt"
/*
  Go program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/

// Check that prefix of string is part of palindrome or not
func isPrefixPalindrome(text string, n int) bool {
	var i int = 0
	var j int = n
	for (i < j) {
		if text[i] != text[j] {
			// When not a palindrome
			return false
		}
		i++
		j--
	}
	// When yes
	return true
}
func makePalindromeByPerpend(text string) {
	if len(text) == 0 {
		return
	}
	var count int = -1
	// Get the length of text
	var n int = len(text)
	// Executing this loop through by size of text length
	for i := n - 1 ; i >= 0 && count == -1 ; i-- {
		if text[0] == text[i] && isPrefixPalindrome(text, i) {
			// When Prefix 0..i is palindrome
			count = (n - 1) - i
		}
	}
	// Display given text
	fmt.Println(" Given Text : ", text)
	fmt.Println(" Need to Perpend ", count, " Characters")
}
func main() {
	// Test Inputs
	// Case A
	// Text : ipid
	//  d ipi d  [Add d at beginning] 
	// dipid => palindrome
	// Result :1
	makePalindromeByPerpend("ipid")
	// Case B
	// Text : 123321892
	// 298 123321 892 [Add 298 at beginning]
	// 298123321892 => palindrome
	// Result : 3
	makePalindromeByPerpend("123321892")
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	makePalindromeByPerpend("121")
	// Case D
	// Text : 12356
	// 6532 1 2356 
	// Result : 4
	makePalindromeByPerpend("12356")
}

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
<?php
/*
  Php program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
class Palindrome
{
	// Check that prefix of string is part of palindrome or not
	public	function isPrefixPalindrome($text, $n)
	{
		$i = 0;
		$j = $n;
		while ($i < $j)
		{
			if ($text[$i] != $text[$j])
			{
				// When not a palindrome
				return false;
			}
			$i++;
			$j--;
		}
		// When yes
		return true;
	}
	public	function makePalindromeByPerpend($text)
	{
		if (strlen($text) == 0)
		{
			return;
		}
		$count = -1;
		// Get the length of text
		$n = strlen($text);
		// Executing this loop through by size of text length
		for ($i = $n - 1; $i >= 0 && $count == -1; --$i)
		{
			if ($text[0] == $text[$i] && 
                $this->isPrefixPalindrome($text, $i))
			{
				// When Prefix 0..i is palindrome
				$count = ($n - 1) - $i;
			}
		}
		// Display given text
		echo(" Given Text : ".$text.
			"\n");
		echo(" Need to Perpend ".$count.
			" Characters".
			"\n");
	}
}

function main()
{
	$task = new Palindrome();
	// Test Inputs
	// Case A
	// Text : ipid
	//  d ipi d  [Add d at beginning] 
	// dipid => palindrome
	// Result :1
	$task->makePalindromeByPerpend("ipid");
	// Case B
	// Text : 123321892
	// 298 123321 892 [Add 298 at beginning]
	// 298123321892 => palindrome
	// Result : 3
	$task->makePalindromeByPerpend("123321892");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	$task->makePalindromeByPerpend("121");
	// Case D
	// Text : 12356
	// 6532 1 2356 
	// Result : 4
	$task->makePalindromeByPerpend("12356");
}
main();

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
/*
  Node JS program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
class Palindrome
{
	// Check that prefix of string is part of palindrome or not
	isPrefixPalindrome(text, n)
	{
		var i = 0;
		var j = n;
		while (i < j)
		{
			if (text.charAt(i) != text.charAt(j))
			{
				// When not a palindrome
				return false;
			}
			i++;
			j--;
		}
		// When yes
		return true;
	}
	makePalindromeByPerpend(text)
	{
		if (text.length == 0)
		{
			return;
		}
		var count = -1;
		// Get the length of text
		var n = text.length;
		// Executing this loop through by size of text length
		for (var i = n - 1; i >= 0 && count == -1; --i)
		{
			if (text.charAt(0) == text.charAt(i) && 
                this.isPrefixPalindrome(text, i))
			{
				// When Prefix 0..i is palindrome
				count = (n - 1) - i;
			}
		}
		// Display given text
		console.log(" Given Text : " + text);
		console.log(" Need to Perpend " + count + " Characters");
	}
}

function main()
{
	var task = new Palindrome();
	// Test Inputs
	// Case A
	// Text : ipid
	//  d ipi d  [Add d at beginning] 
	// dipid => palindrome
	// Result :1
	task.makePalindromeByPerpend("ipid");
	// Case B
	// Text : 123321892
	// 298 123321 892 [Add 298 at beginning]
	// 298123321892 => palindrome
	// Result : 3
	task.makePalindromeByPerpend("123321892");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task.makePalindromeByPerpend("121");
	// Case D
	// Text : 12356
	// 6532 1 2356 
	// Result : 4
	task.makePalindromeByPerpend("12356");
}
main();

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
#  Python 3 program for
#  Minimum number of perpend character needed to 
#  Make a string palindrome
class Palindrome :
	#  Check that prefix of string is part of palindrome or not
	def isPrefixPalindrome(self, text, n) :
		i = 0
		j = n
		while (i < j) :
			if (text[i] != text[j]) :
				#  When not a palindrome
				return False
			
			i += 1
			j -= 1
		
		#  When yes
		return True
	
	def makePalindromeByPerpend(self, text) :
		if (len(text) == 0) :
			return
		
		count = -1
		#  Get the length of text
		n = len(text)
		i = n - 1
		#  Executing this loop through by size of text length
		while (i >= 0 and count == -1) :
			if (text[0] == text[i] and 
                self.isPrefixPalindrome(text, i)) :
				#  When Prefix 0..i is palindrome
				count = (n - 1) - i
			
			i -= 1
		
		#  Display given text
		print(" Given Text : ", text)
		print(" Need to Perpend ", count ," Characters")
	

def main() :
	task = Palindrome()
	#  Test Inputs
	#  Case A
	#  Text : ipid
	#   d ipi d  [Add d at beginning] 
	#  dipid => palindrome
	#  Result :1
	task.makePalindromeByPerpend("ipid")
	#  Case B
	#  Text : 123321892
	#  298 123321 892 [Add 298 at beginning]
	#  298123321892 => palindrome
	#  Result : 3
	task.makePalindromeByPerpend("123321892")
	#  Case C
	#  Text : 121
	#  121 it's already palindrome
	#  Result : 0
	task.makePalindromeByPerpend("121")
	#  Case D
	#  Text : 12356
	#  6532 1 2356 
	#  Result : 4
	task.makePalindromeByPerpend("12356")

if __name__ == "__main__": main()

Output

 Given Text :  ipid
 Need to Perpend  1  Characters
 Given Text :  123321892
 Need to Perpend  3  Characters
 Given Text :  121
 Need to Perpend  0  Characters
 Given Text :  12356
 Need to Perpend  4  Characters
#  Ruby program for
#  Minimum number of perpend character needed to 
#  Make a string palindrome

class Palindrome 
	#  Check that prefix of string is part of palindrome or not
	def isPrefixPalindrome(text, n) 
		i = 0
		j = n
		while (i < j) 
			if (text[i] != text[j]) 
				#  When not a palindrome
				return false
			end

			i += 1
			j -= 1
		end

		#  When yes
		return true
	end

	def makePalindromeByPerpend(text) 
		if (text.length == 0) 
			return
		end

		count = -1
		#  Get the length of text
		n = text.length
		i = n - 1
		#  Executing this loop through by size of text length
		while (i >= 0 && count == -1) 
			if (text[0] == text[i] && 
                self.isPrefixPalindrome(text, i)) 
				#  When Prefix 0..i is palindrome
				count = (n - 1) - i
			end

			i -= 1
		end

		#  Display given text
		print(" Given Text : ", text, "\n")
		print(" Need to Perpend ", count ," Characters", "\n")
	end

end

def main() 
	task = Palindrome.new()
	#  Test Inputs
	#  Case A
	#  Text : ipid
	#   d ipi d  [Add d at beginning] 
	#  dipid => palindrome
	#  Result :1
	task.makePalindromeByPerpend("ipid")
	#  Case B
	#  Text : 123321892
	#  298 123321 892 [Add 298 at beginning]
	#  298123321892 => palindrome
	#  Result : 3
	task.makePalindromeByPerpend("123321892")
	#  Case C
	#  Text : 121
	#  121 it's already palindrome
	#  Result : 0
	task.makePalindromeByPerpend("121")
	#  Case D
	#  Text : 12356
	#  6532 1 2356 
	#  Result : 4
	task.makePalindromeByPerpend("12356")
end

main()

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
import scala.collection.mutable._;
/*
  Scala program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
class Palindrome()
{
	// Check that prefix of string is part of palindrome or not
	def isPrefixPalindrome(text: String, n: Int): Boolean = {
		var i: Int = 0;
		var j: Int = n;
		while (i < j)
		{
			if (text.charAt(i) != text.charAt(j))
			{
				// When not a palindrome
				return false;
			}
			i += 1;
			j -= 1;
		}
		// When yes
		return true;
	}
	def makePalindromeByPerpend(text: String): Unit = {
		if (text.length() == 0)
		{
			return;
		}
		var count: Int = -1;
		// Get the length of text
		var n: Int = text.length();
		var i: Int = n - 1;
		// Executing this loop through by size of text length
		while (i >= 0 && count == -1)
		{
			if (text.charAt(0) == text.charAt(i) && 
                isPrefixPalindrome(text, i))
			{
				// When Prefix 0..i is palindrome
				count = (n - 1) - i;
			}
			i -= 1;
		}
		// Display given text
		println(" Given Text : " + text);
		println(" Need to Perpend " + count + " Characters");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Palindrome = new Palindrome();
		// Test Inputs
		// Case A
		// Text : ipid
		//  d ipi d  [Add d at beginning] 
		// dipid => palindrome
		// Result :1
		task.makePalindromeByPerpend("ipid");
		// Case B
		// Text : 123321892
		// 298 123321 892 [Add 298 at beginning]
		// 298123321892 => palindrome
		// Result : 3
		task.makePalindromeByPerpend("123321892");
		// Case C
		// Text : 121
		// 121 it's already palindrome
		// Result : 0
		task.makePalindromeByPerpend("121");
		// Case D
		// Text : 12356
		// 6532 1 2356 
		// Result : 4
		task.makePalindromeByPerpend("12356");
	}
}

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters
import Foundation;
/*
  Swift 4 program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
class Palindrome
{
	// Check that prefix of string is part of palindrome or not
	func isPrefixPalindrome(_ text: [Character], _ n: Int) -> Bool
	{
		var i: Int = 0;
		var j: Int = n;
		while (i < j)
		{
			if (text[i]  != text[j])
			{
				// When not a palindrome
				return false;
			}
			i += 1;
			j -= 1;
		}
		// When yes
		return true;
	}
	func makePalindromeByPerpend(_ data: String)
	{
		if (data.count == 0)
		{
			return;
		}
		var count: Int = -1;
		// Get the length of text
		let n: Int = data.count;
		var i: Int = n - 1;
        let text = Array(data);
		// Executing this loop through by size of text length
		while (i >= 0 && count == -1)
		{
			if (text[0] == text[i] && 
                self.isPrefixPalindrome(text, i))
			{
				// When Prefix 0..i is palindrome
				count = (n - 1) - i;
			}
			i -= 1;
		}
		// Display given text
		print(" Given Text : ", data);
		print(" Need to Perpend", count ,"Characters");
	}
}
func main()
{
	let task: Palindrome = Palindrome();
	// Test Inputs
	// Case A
	// Text : ipid
	//  d ipi d  [Add d at beginning] 
	// dipid => palindrome
	// Result :1
	task.makePalindromeByPerpend("ipid");
	// Case B
	// Text : 123321892
	// 298 123321 892 [Add 298 at beginning]
	// 298123321892 => palindrome
	// Result : 3
	task.makePalindromeByPerpend("123321892");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task.makePalindromeByPerpend("121");
	// Case D
	// Text : 12356
	// 6532 1 2356 
	// Result : 4
	task.makePalindromeByPerpend("12356");
}
main();

Output

 Given Text :  ipid
 Need to Perpend 1 Characters
 Given Text :  123321892
 Need to Perpend 3 Characters
 Given Text :  121
 Need to Perpend 0 Characters
 Given Text :  12356
 Need to Perpend 4 Characters
/*
  Kotlin program for
  Minimum number of perpend character needed to 
  Make a string palindrome
*/
class Palindrome
{
	// Check that prefix of string is part of palindrome or not
	fun isPrefixPalindrome(text: String, n: Int): Boolean
	{
		var i: Int = 0;
		var j: Int = n;
		while (i < j)
		{
			if (text.get(i) != text.get(j))
			{
				// When not a palindrome
				return false;
			}
			i += 1;
			j -= 1;
		}
		// When yes
		return true;
	}
	fun makePalindromeByPerpend(text: String): Unit
	{
		if (text.length == 0)
		{
			return;
		}
		var count: Int = -1;
		// Get the length of text
		val n: Int = text.length;
		var i: Int = n - 1;
		// Executing this loop through by size of text length
		while (i >= 0 && count == -1)
		{
			if (text.get(0) == text.get(i) && 
                this.isPrefixPalindrome(text, i))
			{
				// When Prefix 0..i is palindrome
				count = (n - 1) - i;
			}
			i -= 1;
		}
		// Display given text
		println(" Given Text : " + text);
		println(" Need to Perpend " + count + " Characters");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Palindrome = Palindrome();
	// Test Inputs
	// Case A
	// Text : ipid
	//  d ipi d  [Add d at beginning] 
	// dipid => palindrome
	// Result :1
	task.makePalindromeByPerpend("ipid");
	// Case B
	// Text : 123321892
	// 298 123321 892 [Add 298 at beginning]
	// 298123321892 => palindrome
	// Result : 3
	task.makePalindromeByPerpend("123321892");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task.makePalindromeByPerpend("121");
	// Case D
	// Text : 12356
	// 6532 1 2356 
	// Result : 4
	task.makePalindromeByPerpend("12356");
}

Output

 Given Text : ipid
 Need to Perpend 1 Characters
 Given Text : 123321892
 Need to Perpend 3 Characters
 Given Text : 121
 Need to Perpend 0 Characters
 Given Text : 12356
 Need to Perpend 4 Characters




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment