Minimum number of append character needed to make a string palindrome

Here given code implementation process.

/*
  Java program for
  Minimum number of append character needed to make a string palindrome
*/
public class Palindrome
{
	// Check that suffix of string is part of palindrome or not
	public boolean isSuffixPalindrome(String text, int s, int e)
	{
		int i = s;
		int j = e;
		while (i < j)
		{
			if (text.charAt(i) != text.charAt(j))
			{
				// When not a palindrome
				return false;
			}
			i++;
			j--;
		}
		// When yes
		return true;
	}
	public void makePalindromeByAppend(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 = 0; i < n && count == -1; ++i)
		{
			if (text.charAt(i) == text.charAt(n - 1) && 
                isSuffixPalindrome(text, i, n - 1))
			{
				// When suffix i..n is palindrome
				count = i;
			}
		}
		// Display given text
		System.out.println(" Given Text : " + text);
		System.out.println(" Need to append " + count + " characters");
	}
	public static void main(String[] args)
	{
		Palindrome task = new Palindrome();
		// Test Inputs
		// Case A
		// Text : 123443
		//  12  3443 21  [Add 21 at end] 
		// 12344321 => palindrome
		// Result : 2
		task.makePalindromeByAppend("123443");
		// Case B
		// Text : faafcdc
		// faaf cdc faaf [Add faaf at end]
		// faafcdcfaaf => palindrome
		// Result : 4
		task.makePalindromeByAppend("faafcdc");
		// Case C
		// Text : 121
		// 121 it's already palindrome
		// Result : 0
		task.makePalindromeByAppend("121");
		// Case D
		// Text : 1236
		// 123 6 321 
		// Result : 3
		task.makePalindromeByAppend("1236");
	}
}

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
  C++ program for
  Minimum number of append character needed to make a string palindrome
*/
class Palindrome
{
	public:
		// Check that suffix of string is part of palindrome or not
		bool isSuffixPalindrome(string text, int s, int e)
		{
			int i = s;
			int j = e;
			while (i < j)
			{
				if (text[i] != text[j])
				{
					// When not a palindrome
					return false;
				}
				i++;
				j--;
			}
			// When yes
			return true;
		}
	void makePalindromeByAppend(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 = 0; i < n && count == -1; ++i)
		{
			if (text[i] == text[n - 1] && 
                this->isSuffixPalindrome(text, i, n - 1))
			{
				// When suffix i..n is palindrome
				count = i;
			}
		}
		// Display given text
		cout << " Given Text : " << text << endl;
		cout << " Need to append " << count << " characters" << endl;
	}
};
int main()
{
	Palindrome *task = new Palindrome();
	// Test Inputs
	// Case A
	// Text : 123443
	//  12  3443 21  [Add 21 at end] 
	// 12344321 => palindrome
	// Result : 2
	task->makePalindromeByAppend("123443");
	// Case B
	// Text : faafcdc
	// faaf cdc faaf [Add faaf at end]
	// faafcdcfaaf => palindrome
	// Result : 4
	task->makePalindromeByAppend("faafcdc");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task->makePalindromeByAppend("121");
	// Case D
	// Text : 1236
	// 123 6 321 
	// Result : 3
	task->makePalindromeByAppend("1236");
	return 0;
}

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
// Include namespace system
using System;
/*
  Csharp program for
  Minimum number of append character needed to make a string palindrome
*/
public class Palindrome
{
	// Check that suffix of string is part of palindrome or not
	public Boolean isSuffixPalindrome(String text, int s, int e)
	{
		int i = s;
		int j = e;
		while (i < j)
		{
			if (text[i] != text[j])
			{
				// When not a palindrome
				return false;
			}
			i++;
			j--;
		}
		// When yes
		return true;
	}
	public void makePalindromeByAppend(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 = 0; i < n && count == -1; ++i)
		{
			if (text[i] == text[n - 1] && 
                this.isSuffixPalindrome(text, i, n - 1))
			{
				// When suffix i..n is palindrome
				count = i;
			}
		}
		// Display given text
		Console.WriteLine(" Given Text : " + text);
		Console.WriteLine(" Need to append " + count + " characters");
	}
	public static void Main(String[] args)
	{
		Palindrome task = new Palindrome();
		// Test Inputs
		// Case A
		// Text : 123443
		//  12  3443 21  [Add 21 at end] 
		// 12344321 => palindrome
		// Result : 2
		task.makePalindromeByAppend("123443");
		// Case B
		// Text : faafcdc
		// faaf cdc faaf [Add faaf at end]
		// faafcdcfaaf => palindrome
		// Result : 4
		task.makePalindromeByAppend("faafcdc");
		// Case C
		// Text : 121
		// 121 it's already palindrome
		// Result : 0
		task.makePalindromeByAppend("121");
		// Case D
		// Text : 1236
		// 123 6 321 
		// Result : 3
		task.makePalindromeByAppend("1236");
	}
}

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
package main
import "fmt"
/*
  Go program for
  Minimum number of append character needed to make a string palindrome
*/
// Check that suffix of string is part of palindrome or not
func isSuffixPalindrome(text string, s int, e int) bool {
	var i int = s
	var j int = e
	for (i < j) {
		if text[i] != text[j] {
			// When not a palindrome
			return false
		}
		i++
		j--
	}
	// When yes
	return true
}
func makePalindromeByAppend(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 := 0 ; i < n && count == -1 ; i++ {
		if text[i] == text[n - 1] && isSuffixPalindrome(text, i, n - 1) {
			// When suffix i..n is palindrome
			count = i
		}
	}
	// Display given text
	fmt.Println(" Given Text : ", text)
	fmt.Println(" Need to append ", count, " characters")
}
func main() {
	
	// Test Inputs
	// Case A
	// Text : 123443
	//  12  3443 21  [Add 21 at end] 
	// 12344321 => palindrome
	// Result : 2
	makePalindromeByAppend("123443")
	// Case B
	// Text : faafcdc
	// faaf cdc faaf [Add faaf at end]
	// faafcdcfaaf => palindrome
	// Result : 4
	makePalindromeByAppend("faafcdc")
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	makePalindromeByAppend("121")
	// Case D
	// Text : 1236
	// 123 6 321 
	// Result : 3
	makePalindromeByAppend("1236")
}

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
<?php
/*
  Php program for
  Minimum number of append character needed to make a string palindrome
*/
class Palindrome
{
	// Check that suffix of string is part of palindrome or not
	public	function isSuffixPalindrome($text, $s, $e)
	{
		$i = $s;
		$j = $e;
		while ($i < $j)
		{
			if ($text[$i] != $text[$j])
			{
				// When not a palindrome
				return false;
			}
			$i++;
			$j--;
		}
		// When yes
		return true;
	}
	public	function makePalindromeByAppend($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 = 0; $i < $n && $count == -1; ++$i)
		{
			if ($text[$i] == $text[$n - 1] && 
                $this->isSuffixPalindrome($text, $i, $n - 1))
			{
				// When suffix i..n is palindrome
				$count = $i;
			}
		}
		// Display given text
		echo(" Given Text : ".$text."\n");
		echo(" Need to append ".$count." characters\n");
	}
}

function main()
{
	$task = new Palindrome();
	// Test Inputs
	// Case A
	// Text : 123443
	//  12  3443 21  [Add 21 at end] 
	// 12344321 => palindrome
	// Result : 2
	$task->makePalindromeByAppend("123443");
	// Case B
	// Text : faafcdc
	// faaf cdc faaf [Add faaf at end]
	// faafcdcfaaf => palindrome
	// Result : 4
	$task->makePalindromeByAppend("faafcdc");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	$task->makePalindromeByAppend("121");
	// Case D
	// Text : 1236
	// 123 6 321 
	// Result : 3
	$task->makePalindromeByAppend("1236");
}
main();

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
/*
  Node JS program for
  Minimum number of append character needed to make a string palindrome
*/
class Palindrome
{
	// Check that suffix of string is part of palindrome or not
	isSuffixPalindrome(text, s, e)
	{
		var i = s;
		var j = e;
		while (i < j)
		{
			if (text.charAt(i) != text.charAt(j))
			{
				// When not a palindrome
				return false;
			}
			i++;
			j--;
		}
		// When yes
		return true;
	}
	makePalindromeByAppend(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 = 0; i < n && count == -1; ++i)
		{
			if (text.charAt(i) == text.charAt(n - 1) && 
                this.isSuffixPalindrome(text, i, n - 1))
			{
				// When suffix i..n is palindrome
				count = i;
			}
		}
		// Display given text
		console.log(" Given Text : " + text);
		console.log(" Need to append " + count + " characters");
	}
}

function main()
{
	var task = new Palindrome();
	// Test Inputs
	// Case A
	// Text : 123443
	//  12  3443 21  [Add 21 at end] 
	// 12344321 => palindrome
	// Result : 2
	task.makePalindromeByAppend("123443");
	// Case B
	// Text : faafcdc
	// faaf cdc faaf [Add faaf at end]
	// faafcdcfaaf => palindrome
	// Result : 4
	task.makePalindromeByAppend("faafcdc");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task.makePalindromeByAppend("121");
	// Case D
	// Text : 1236
	// 123 6 321 
	// Result : 3
	task.makePalindromeByAppend("1236");
}
main();

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
#  Python 3 program for
#  Minimum number of append character needed to make a string palindrome
class Palindrome :
	#  Check that suffix of string is part of palindrome or not
	def isSuffixPalindrome(self, text, s, e) :
		i = s
		j = e
		while (i < j) :
			if (text[i] != text[j]) :
				#  When not a palindrome
				return False
			
			i += 1
			j -= 1
		
		#  When yes
		return True
	
	def makePalindromeByAppend(self, text) :
		if (len(text) == 0) :
			return
		
		count = -1
		#  Get the length of text
		n = len(text)
		i = 0
		#  Executing this loop through by size of text length
		while (i < n and count == -1) :
			if (text[i] == text[n - 1] and 
                self.isSuffixPalindrome(text, i, n - 1)) :
				#  When suffix i..n is palindrome
				count = i
			
			i += 1
		
		#  Display given text
		print(" Given Text : ", text)
		print(" Need to append ", count ," characters")
	

def main() :
	task = Palindrome()
	#  Test Inputs
	#  Case A
	#  Text : 123443
	#   12  3443 21  [Add 21 at end] 
	#  12344321 => palindrome
	#  Result : 2
	task.makePalindromeByAppend("123443")
	#  Case B
	#  Text : faafcdc
	#  faaf cdc faaf [Add faaf at end]
	#  faafcdcfaaf => palindrome
	#  Result : 4
	task.makePalindromeByAppend("faafcdc")
	#  Case C
	#  Text : 121
	#  121 it's already palindrome
	#  Result : 0
	task.makePalindromeByAppend("121")
	#  Case D
	#  Text : 1236
	#  123 6 321 
	#  Result : 3
	task.makePalindromeByAppend("1236")

if __name__ == "__main__": main()

Output

 Given Text :  123443
 Need to append  2  characters
 Given Text :  faafcdc
 Need to append  4  characters
 Given Text :  121
 Need to append  0  characters
 Given Text :  1236
 Need to append  3  characters
#  Ruby program for
#  Minimum number of append character needed to make a string palindrome
class Palindrome 
	#  Check that suffix of string is part of palindrome or not
	def isSuffixPalindrome(text, s, e) 
		i = s
		j = e
		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 makePalindromeByAppend(text) 
		if (text.length == 0) 
			return
		end

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

			i += 1
		end

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

end

def main() 
	task = Palindrome.new()
	#  Test Inputs
	#  Case A
	#  Text : 123443
	#   12  3443 21  [Add 21 at end] 
	#  12344321 => palindrome
	#  Result : 2
	task.makePalindromeByAppend("123443")
	#  Case B
	#  Text : faafcdc
	#  faaf cdc faaf [Add faaf at end]
	#  faafcdcfaaf => palindrome
	#  Result : 4
	task.makePalindromeByAppend("faafcdc")
	#  Case C
	#  Text : 121
	#  121 it's already palindrome
	#  Result : 0
	task.makePalindromeByAppend("121")
	#  Case D
	#  Text : 1236
	#  123 6 321 
	#  Result : 3
	task.makePalindromeByAppend("1236")
end

main()

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
import scala.collection.mutable._;
/*
  Scala program for
  Minimum number of append character needed to make a string palindrome
*/
class Palindrome()
{
	// Check that suffix of string is part of palindrome or not
	def isSuffixPalindrome(text: String, s: Int, e: Int): Boolean = {
		var i: Int = s;
		var j: Int = e;
		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 makePalindromeByAppend(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 = 0;
		// Executing this loop through by size of text length
		while (i < n && count == -1)
		{
			if (text.charAt(i) == text.charAt(n - 1) && 
                isSuffixPalindrome(text, i, n - 1))
			{
				// When suffix i..n is palindrome
				count = i;
			}
			i += 1;
		}
		// Display given text
		println(" Given Text : " + text);
		println(" Need to append " + count + " characters");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Palindrome = new Palindrome();
		// Test Inputs
		// Case A
		// Text : 123443
		//  12  3443 21  [Add 21 at end] 
		// 12344321 => palindrome
		// Result : 2
		task.makePalindromeByAppend("123443");
		// Case B
		// Text : faafcdc
		// faaf cdc faaf [Add faaf at end]
		// faafcdcfaaf => palindrome
		// Result : 4
		task.makePalindromeByAppend("faafcdc");
		// Case C
		// Text : 121
		// 121 it's already palindrome
		// Result : 0
		task.makePalindromeByAppend("121");
		// Case D
		// Text : 1236
		// 123 6 321 
		// Result : 3
		task.makePalindromeByAppend("1236");
	}
}

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters
import Foundation;
/*
  Swift 4 program for
  Minimum number of append character needed to make a string palindrome
*/
class Palindrome
{
	// Check that suffix of string is part of palindrome or not
	func isSuffixPalindrome(_ text: [Character], _ s: Int, _ e: Int) -> Bool
	{
		var i: Int = s;
		var j: Int = e;
		while (i < j)
		{
			if (text[i]  != text[j])
			{
				// When not a palindrome
				return false;
			}
			i += 1;
			j -= 1;
		}
		// When yes
		return true;
	}
	func makePalindromeByAppend(_ data: String)
	{
		if (data.count == 0)
		{
			return;
		}
		var count: Int = -1;
		// Get the length of text
		let n: Int = data.count;
		var i: Int = 0;
      	let text = Array(data);
		// Executing this loop through by size of text length
		while (i < n && count == -1)
		{
			if (text[i] == text[n - 1] && 
                self.isSuffixPalindrome(text, i, n - 1))
			{
				// When suffix i..n is palindrome
				count = i;
			}
			i += 1;
		}
		// Display given text
		print(" Given Text : ", data);
		print(" Need to append ", count ," characters");
	}
}
func main()
{
	let task: Palindrome = Palindrome();
	// Test Inputs
	// Case A
	// Text : 123443
	//  12  3443 21  [Add 21 at end] 
	// 12344321 => palindrome
	// Result : 2
	task.makePalindromeByAppend("123443");
	// Case B
	// Text : faafcdc
	// faaf cdc faaf [Add faaf at end]
	// faafcdcfaaf => palindrome
	// Result : 4
	task.makePalindromeByAppend("faafcdc");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task.makePalindromeByAppend("121");
	// Case D
	// Text : 1236
	// 123 6 321 
	// Result : 3
	task.makePalindromeByAppend("1236");
}
main();

Output

 Given Text :  123443
 Need to append  2  characters
 Given Text :  faafcdc
 Need to append  4  characters
 Given Text :  121
 Need to append  0  characters
 Given Text :  1236
 Need to append  3  characters
/*
  Kotlin program for
  Minimum number of append character needed to make a string palindrome
*/
class Palindrome
{
	// Check that suffix of string is part of palindrome or not
	fun isSuffixPalindrome(text: String, s: Int, e: Int): Boolean
	{
		var i: Int = s;
		var j: Int = e;
		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 makePalindromeByAppend(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 = 0;
		// Executing this loop through by size of text length
		while (i < n && count == -1)
		{
			if (text.get(i) == text.get(n - 1) && 
                this.isSuffixPalindrome(text, i, n - 1))
			{
				// When suffix i..n is palindrome
				count = i;
			}
			i += 1;
		}
		// Display given text
		println(" Given Text : " + text);
		println(" Need to append " + count + " characters");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Palindrome = Palindrome();
	// Test Inputs
	// Case A
	// Text : 123443
	//  12  3443 21  [Add 21 at end] 
	// 12344321 => palindrome
	// Result : 2
	task.makePalindromeByAppend("123443");
	// Case B
	// Text : faafcdc
	// faaf cdc faaf [Add faaf at end]
	// faafcdcfaaf => palindrome
	// Result : 4
	task.makePalindromeByAppend("faafcdc");
	// Case C
	// Text : 121
	// 121 it's already palindrome
	// Result : 0
	task.makePalindromeByAppend("121");
	// Case D
	// Text : 1236
	// 123 6 321 
	// Result : 3
	task.makePalindromeByAppend("1236");
}

Output

 Given Text : 123443
 Need to append 2 characters
 Given Text : faafcdc
 Need to append 4 characters
 Given Text : 121
 Need to append 0 characters
 Given Text : 1236
 Need to append 3 characters


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







© 2021, kalkicode.com, All rights reserved