Longest palindromic substring solution

Here given code implementation process.

/*
    C program for
    Longest palindromic substring solution
*/
#include <stdio.h>
#include <string.h>

// Returns length of palindrome in given indexes
int palindromeLength(char *text, int l, int r, int n)
{
	int low = l;
	int high = r;
	int count = 0;
	while (low >= 0 && high < n && text[low] == text[high])
	{
		// When palindrome pairs exist
		count += 2;
		// Change position
		low--;
		high++;
	}
	return count;
}
void findLPSS(char *text)
{
	// Get the length of given text
	int n = strlen(text);
	if (n == 0)
	{
		return;
	}
	// Every single character is form of palindrome.
	// So set result length is 1.
	int length = 1;
	// Auxiliary variables
	int start = 0;
	int a = 0;
	int b = 0;
	// Find the max length palindrome.
	// Execute loop from 1 to n-1.
	for (int i = 1; i < n; ++i)
	{
		// Case A
		// Use to maintain order of left and right elements.
		// i is center point.
		// r consider right side boundary.
		// And l consider left side boundary.
		// Current boundary position
		// l = i - 1
		// r = i + 1
		a = palindromeLength(text, i - 1, i + 1, n) + 1;
		// Case B
		// When [i] location is not center of palindrome so test with change of boundary. 
		// Current boundary position
		// l = i - 1
		// r = i 
		b = palindromeLength(text, i - 1, i, n);
		if (b > a)
		{
			// Select largest
			a = b;
		}
		if (a > length)
		{
			// Change resultant length
			length = a;
			start = i - (a / 2);
		}
	}
	// Display given text
	printf("\n Given string : %s", text);
	printf("\n Result : ");
	// Display resultant palindrome
	for (int i = start; i <= start + length - 1; ++i)
	{
		printf("%c", text[i]);
	}
	// Display length of resultant palindrome
	printf("\n Resultant length : %d", length);
}
int main(int argc, char const *argv[])
{
	// Test
	// iiii
	findLPSS("itiiiigi");
	// ivfvi
	findLPSS("vvvvivfvim");
	// civic
	findLPSS("xyzacivicxrs");
	// opo
	findLPSS("ttoopo");
	return 0;
}

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
// Java program for
// Longest palindromic substring solution
public class LongestPalindrome
{
	// Returns length of palindrome in given indexes
	public int palindromeLength(String text, int l, int r, int n)
	{
		int low = l;
		int high = r;
		int count = 0;
		while (low >= 0 && high < n && 
               text.charAt(low) == text.charAt(high))
		{
			// When palindrome pairs exist
			count += 2;
			// Change position
			low--;
			high++;
		}
		return count;
	}
	public void findLPSS(String text)
	{
		// Get the length of given text
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		int length = 1;
		// Auxiliary variables
		int start = 0;
		int a = 0;
		int b = 0;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		for (int i = 1; i < n; ++i)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			a = palindromeLength(text, i - 1, i + 1, n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
          	// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			b = palindromeLength(text, i - 1, i, n);
			if (b > a)
			{
				// Select largest
				a = b;
			}
			if (a > length)
			{
				// Change resultant length
				length = a;
				start = i - (a / 2);
			}
		}
		// Display given text
		System.out.print("\n Given string : " + text);
		System.out.print("\n Result : ");
		// Display resultant palindrome
		for (int i = start; i <= start + length - 1; ++i)
		{
			System.out.print(text.charAt(i));
		}
		// Display length of resultant palindrome
		System.out.print("\n Resultant length : " + length);
	}
	public static void main(String[] args)
	{
		LongestPalindrome task = new LongestPalindrome();
		// Test
		// iiii
		task.findLPSS("itiiiigi");
		// ivfvi
		task.findLPSS("vvvvivfvim");
		// civic
		task.findLPSS("xyzacivicxrs");
		// ttoopo
		task.findLPSS("ttoopo");
	}
}

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Longest palindromic substring solution
class LongestPalindrome
{
	public:
		// Returns length of palindrome in given indexes
		int palindromeLength(string text, int l, int r, int n)
		{
			int low = l;
			int high = r;
			int count = 0;
			while (low >= 0 && high < n && text[low] == text[high])
			{
				// When palindrome pairs exist
				count += 2;
				// Change position
				low--;
				high++;
			}
			return count;
		}
	void findLPSS(string text)
	{
		// Get the length of given text
		int n = text.length();
		if (n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		int length = 1;
		// Auxiliary variables
		int start = 0;
		int a = 0;
		int b = 0;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		for (int i = 1; i < n; ++i)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			a = this->palindromeLength(text, i - 1, i + 1, n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
			// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			b = this->palindromeLength(text, i - 1, i, n);
			if (b > a)
			{
				// Select largest
				a = b;
			}
			if (a > length)
			{
				// Change resultant length
				length = a;
				start = i - (a / 2);
			}
		}
		// Display given text
		cout << "\n Given string : " << text;
		cout << "\n Result : ";
		// Display resultant palindrome
		for (int i = start; i <= start + length - 1; ++i)
		{
			cout << text[i];
		}
		// Display length of resultant palindrome
		cout << "\n Resultant length : " << length;
	}
};
int main()
{
	LongestPalindrome *task = new LongestPalindrome();
	// Test
	// iiii
	task->findLPSS("itiiiigi");
	// ivfvi
	task->findLPSS("vvvvivfvim");
	// civic
	task->findLPSS("xyzacivicxrs");
	// opo
	task->findLPSS("ttoopo");
	return 0;
}

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
// Include namespace system
using System;
// Csharp program for
// Longest palindromic substring solution
public class LongestPalindrome
{
	// Returns length of palindrome in given indexes
	public int palindromeLength(String text, int l, int r, int n)
	{
		int low = l;
		int high = r;
		int count = 0;
		while (low >= 0 && high < n && text[low] == text[high])
		{
			// When palindrome pairs exist
			count += 2;
			// Change position
			low--;
			high++;
		}
		return count;
	}
	public void findLPSS(String text)
	{
		// Get the length of given text
		int n = text.Length;
		if (n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		int length = 1;
		// Auxiliary variables
		int start = 0;
		int a = 0;
		int b = 0;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		for (int i = 1; i < n; ++i)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			a = this.palindromeLength(text, i - 1, i + 1, n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
			// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			b = this.palindromeLength(text, i - 1, i, n);
			if (b > a)
			{
				// Select largest
				a = b;
			}
			if (a > length)
			{
				// Change resultant length
				length = a;
				start = i - (a / 2);
			}
		}
		// Display given text
		Console.Write("\n Given string : " + text);
		Console.Write("\n Result : ");
		// Display resultant palindrome
		for (int i = start; i <= start + length - 1; ++i)
		{
			Console.Write(text[i]);
		}
		// Display length of resultant palindrome
		Console.Write("\n Resultant length : " + length);
	}
	public static void Main(String[] args)
	{
		LongestPalindrome task = new LongestPalindrome();
		// Test
		// iiii
		task.findLPSS("itiiiigi");
		// ivfvi
		task.findLPSS("vvvvivfvim");
		// civic
		task.findLPSS("xyzacivicxrs");
		// opo
		task.findLPSS("ttoopo");
	}
}

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
package main
import "fmt"
// Go program for
// Longest palindromic substring solution

// Returns length of palindrome in given indexes
func palindromeLength(text string, l int, r int, n int) int {
	var low int = l
	var high int = r
	var count int = 0
	for (low >= 0 && high < n && text[low] == text[high]) {
		// When palindrome pairs exist
		count += 2
		// Change position
		low--
		high++
	}
	return count
}
func findLPSS(text string) {
	// Get the length of given text
	var n int = len(text)
	if n == 0 {
		return
	}
	// Every single character is form of palindrome.
	// So set result length is 1.
	var length int = 1
	// Auxiliary variables
	var start int = 0
	var a int = 0
	var b int = 0
	// Find the max length palindrome.
	// Execute loop from 1 to n-1.
	for i := 1 ; i < n ; i++ {
		// Case A
		// Use to maintain order of left and right elements.
		// i is center point.
		// r consider right side boundary.
		// And l consider left side boundary.
		// Current boundary position
		// l = i - 1
		// r = i + 1
		a = palindromeLength(text, i - 1, i + 1, n) + 1
		// Case B
		// When [i] location is not center of palindrome 
		// so test with change of boundary. 
		// Current boundary position
		// l = i - 1
		// r = i 
		b = palindromeLength(text, i - 1, i, n)
		if b > a {
			// Select largest
			a = b
		}
		if a > length {
			// Change resultant length
			length = a
			start = i - (a / 2)
		}
	}
	// Display given text
	fmt.Print("\n Given string : ", text)
	fmt.Print("\n Result : ")
	// Display resultant palindrome
	for i := start ; i <= start + length - 1 ; i++ {
		fmt.Print(string(text[i]))
	}
	// Display length of resultant palindrome
	fmt.Print("\n Resultant length : ", length)
}
func main() {
	
	// Test
	// iiii
	findLPSS("itiiiigi")
	// ivfvi
	findLPSS("vvvvivfvim")
	// civic
	findLPSS("xyzacivicxrs")
	// opo
	findLPSS("ttoopo")
}

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
<?php
// Php program for
// Longest palindromic substring solution
class LongestPalindrome
{
	// Returns length of palindrome in given indexes
	public	function palindromeLength($text, $l, $r, $n)
	{
		$low = $l;
		$high = $r;
		$count = 0;
		while ($low >= 0 && $high < $n && $text[$low] == $text[$high])
		{
			// When palindrome pairs exist
			$count += 2;
			// Change position
			$low--;
			$high++;
		}
		return $count;
	}
	public	function findLPSS($text)
	{
		// Get the length of given text
		$n = strlen($text);
		if ($n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		$length = 1;
		// Auxiliary variables
		$start = 0;
		$a = 0;
		$b = 0;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		for ($i = 1; $i < $n; ++$i)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			$a = $this->palindromeLength($text, $i - 1, $i + 1, $n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
			// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			$b = $this->palindromeLength($text, $i - 1, $i, $n);
			if ($b > $a)
			{
				// Select largest
				$a = $b;
			}
			if ($a > $length)
			{
				// Change resultant length
				$length = $a;
				$start = $i - ((int)($a / 2));
			}
		}
		// Display given text
		echo("\n Given string : ".$text);
		echo("\n Result : ");
		// Display resultant palindrome
		for ($i = $start; $i <= $start + $length - 1; ++$i)
		{
			echo($text[$i]);
		}
		// Display length of resultant palindrome
		echo("\n Resultant length : ".$length);
	}
}

function main()
{
	$task = new LongestPalindrome();
	// Test
	// iiii
	$task->findLPSS("itiiiigi");
	// ivfvi
	$task->findLPSS("vvvvivfvim");
	// civic
	$task->findLPSS("xyzacivicxrs");
	// opo
	$task->findLPSS("ttoopo");
}
main();

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
// Node JS program for
// Longest palindromic substring solution
class LongestPalindrome
{
	// Returns length of palindrome in given indexes
	palindromeLength(text, l, r, n)
	{
		var low = l;
		var high = r;
		var count = 0;
		while (low >= 0 && high < n && 
               text.charAt(low) == text.charAt(high))
		{
			// When palindrome pairs exist
			count += 2;
			// Change position
			low--;
			high++;
		}
		return count;
	}
	findLPSS(text)
	{
		// Get the length of given text
		var n = text.length;
		if (n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		var length = 1;
		// Auxiliary variables
		var start = 0;
		var a = 0;
		var b = 0;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		for (var i = 1; i < n; ++i)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			a = this.palindromeLength(text, i - 1, i + 1, n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
			// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			b = this.palindromeLength(text, i - 1, i, n);
			if (b > a)
			{
				// Select largest
				a = b;
			}
			if (a > length)
			{
				// Change resultant length
				length = a;
				start = i - (parseInt(a / 2));
			}
		}
		// Display given text
		process.stdout.write("\n Given string : " + text);
		process.stdout.write("\n Result : ");
		// Display resultant palindrome
		for (var i = start; i <= start + length - 1; ++i)
		{
			process.stdout.write(text.charAt(i));
		}
		// Display length of resultant palindrome
		process.stdout.write("\n Resultant length : " + length);
	}
}

function main()
{
	var task = new LongestPalindrome();
	// Test
	// iiii
	task.findLPSS("itiiiigi");
	// ivfvi
	task.findLPSS("vvvvivfvim");
	// civic
	task.findLPSS("xyzacivicxrs");
	// opo
	task.findLPSS("ttoopo");
}
main();

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
#  Python 3 program for
#  Longest palindromic substring solution
class LongestPalindrome :
	#  Returns length of palindrome in given indexes
	def palindromeLength(self, text, l, r, n) :
		low = l
		high = r
		count = 0
		while (low >= 0 and high < n and text[low] == text[high]) :
			#  When palindrome pairs exist
			count += 2
			#  Change position
			low -= 1
			high += 1
		
		return count
	
	def findLPSS(self, text) :
		#  Get the length of given text
		n = len(text)
		if (n == 0) :
			return
		
		#  Every single character is form of palindrome.
		#  So set result length is 1.
		length = 1
		#  Auxiliary variables
		start = 0
		a = 0
		b = 0
		i = 1
		#  Find the max length palindrome.
		#  Execute loop from 1 to n-1.
		while (i < n) :
			#  Case A
			#  Use to maintain order of left and right elements.
			#  i is center point.
			#  r consider right side boundary.
			#  And l consider left side boundary.
			#  Current boundary position
			#  l = i - 1
			#  r = i + 1
			a = self.palindromeLength(text, i - 1, i + 1, n) + 1
			#  Case B
			#  When [i] location is not center of palindrome 
			#  so test with change of boundary. 
			#  Current boundary position
			#  l = i - 1
			#  r = i 
			b = self.palindromeLength(text, i - 1, i, n)
			if (b > a) :
				#  Select largest
				a = b
			
			if (a > length) :
				#  Change resultant length
				length = a
				start = i - (int(a / 2))
			
			i += 1
		
		#  Display given text
		print("\n Given string : ", text, end = "")
		print("\n Result : ", end = "")
		i = start
		#  Display resultant palindrome
		while (i <= start + length - 1) :
			print(text[i], end = "")
			i += 1
		
		#  Display length of resultant palindrome
		print("\n Resultant length : ", length, end = "")
	

def main() :
	task = LongestPalindrome()
	#  Test
	#  iiii
	task.findLPSS("itiiiigi")
	#  ivfvi
	task.findLPSS("vvvvivfvim")
	#  civic
	task.findLPSS("xyzacivicxrs")
	#  opo
	task.findLPSS("ttoopo")

if __name__ == "__main__": main()

Output

 Given string :  itiiiigi
 Result : iiii
 Resultant length :  4
 Given string :  vvvvivfvim
 Result : ivfvi
 Resultant length :  5
 Given string :  xyzacivicxrs
 Result : civic
 Resultant length :  5
 Given string :  ttoopo
 Result : opo
 Resultant length :  3
#  Ruby program for
#  Longest palindromic substring solution
class LongestPalindrome 
	#  Returns length of palindrome in given indexes
	def palindromeLength(text, l, r, n) 
		low = l
		high = r
		count = 0
		while (low >= 0 && high < n && text[low] == text[high]) 
			#  When palindrome pairs exist
			count += 2
			#  Change position
			low -= 1
			high += 1
		end

		return count
	end

	def findLPSS(text) 
		#  Get the length of given text
		n = text.length
		if (n == 0) 
			return
		end

		#  Every single character is form of palindrome.
		#  So set result length is 1.
		length = 1
		#  Auxiliary variables
		start = 0
		a = 0
		b = 0
		i = 1
		#  Find the max length palindrome.
		#  Execute loop from 1 to n-1.
		while (i < n) 
			#  Case A
			#  Use to maintain order of left and right elements.
			#  i is center point.
			#  r consider right side boundary.
			#  And l consider left side boundary.
			#  Current boundary position
			#  l = i - 1
			#  r = i + 1
			a = self.palindromeLength(text, i - 1, i + 1, n) + 1
			#  Case B
			#  When [i] location is not center of palindrome 
			#  so test with change of boundary. 
			#  Current boundary position
			#  l = i - 1
			#  r = i 
			b = self.palindromeLength(text, i - 1, i, n)
			if (b > a) 
				#  Select largest
				a = b
			end

			if (a > length) 
				#  Change resultant length
				length = a
				start = i - (a / 2)
			end

			i += 1
		end

		#  Display given text
		print("\n Given string : ", text)
		print("\n Result : ")
		i = start
		#  Display resultant palindrome
		while (i <= start + length - 1) 
			print(text[i])
			i += 1
		end

		#  Display length of resultant palindrome
		print("\n Resultant length : ", length)
	end

end

def main() 
	task = LongestPalindrome.new()
	#  Test
	#  iiii
	task.findLPSS("itiiiigi")
	#  ivfvi
	task.findLPSS("vvvvivfvim")
	#  civic
	task.findLPSS("xyzacivicxrs")
	#  opo
	task.findLPSS("ttoopo")
end

main()

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
// Scala program for
// Longest palindromic substring solution
class LongestPalindrome()
{
	// Returns length of palindrome in given indexes
	def palindromeLength(text: String, l: Int, r: Int, n: Int): Int = {
		var low: Int = l;
		var high: Int = r;
		var count: Int = 0;
		while (low >= 0 && high < n && 
      			text.charAt(low) == text.charAt(high))
		{
			// When palindrome pairs exist
			count += 2;
			// Change position
			low -= 1;
			high += 1;
		}
		return count;
	}
	def findLPSS(text: String): Unit = {
		// Get the length of given text
		var n: Int = text.length();
		if (n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		var length: Int = 1;
		// Auxiliary variables
		var start: Int = 0;
		var a: Int = 0;
		var b: Int = 0;
		var i: Int = 1;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		while (i < n)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			a = palindromeLength(text, i - 1, i + 1, n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
			// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			b = palindromeLength(text, i - 1, i, n);
			if (b > a)
			{
				// Select largest
				a = b;
			}
			if (a > length)
			{
				// Change resultant length
				length = a;
				start = i - (a / 2);
			}
			i += 1;
		}
		// Display given text
		print("\n Given string : " + text);
		print("\n Result : ");
		i = start;
		// Display resultant palindrome
		while (i <= start + length - 1)
		{
			print(text.charAt(i));
			i += 1;
		}
		// Display length of resultant palindrome
		print("\n Resultant length : " + length);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: LongestPalindrome = new LongestPalindrome();
		// Test
		// iiii
		task.findLPSS("itiiiigi");
		// ivfvi
		task.findLPSS("vvvvivfvim");
		// civic
		task.findLPSS("xyzacivicxrs");
		// opo
		task.findLPSS("ttoopo");
	}
}

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3
import Foundation;
// Swift 4 program for
// Longest palindromic substring solution
class LongestPalindrome
{
	// Returns length of palindrome in given indexes
	func palindromeLength(_ text: [Character], 
                          _ l: Int, 
                          _ r: Int, 
                          _ n: Int) -> Int
	{
		var low: Int = l;
		var high: Int = r;
		var count: Int = 0;
		while (low >= 0 && high < n && 
               			text[low] == text[high])
		{
			// When palindrome pairs exist
			count += 2;
			// Change position
			low -= 1;
			high += 1;
		}
		return count;
	}
	func findLPSS(_ data: String)
	{
      	let text = Array(data);
		// Get the length of given text
		let n: Int = text.count;
		if (n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		var length: Int = 1;
		// Auxiliary variables
		var start: Int = 0;
		var a: Int = 0;
		var b: Int = 0;
		var i: Int = 1;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		while (i < n)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			a = self.palindromeLength(text, i - 1, i + 1, n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
			// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			b = self.palindromeLength(text, i - 1, i, n);
			if (b > a)
			{
				// Select largest
				a = b;
			}
			if (a > length)
			{
				// Change resultant length
				length = a;
				start = i - (a / 2);
			}
			i += 1;
		}
		// Display given text
		print("\n Given string : ", data, terminator: "");
		print("\n Result : ", terminator: "");
		i = start;
		// Display resultant palindrome
		while (i <= start + length - 1)
		{
			print(text[i], terminator: "");
			i += 1;
		}
		// Display length of resultant palindrome
		print("\n Resultant length : ", length, terminator: "");
	}
}
func main()
{
	let task: LongestPalindrome = LongestPalindrome();
	// Test
	// iiii
	task.findLPSS("itiiiigi");
	// ivfvi
	task.findLPSS("vvvvivfvim");
	// civic
	task.findLPSS("xyzacivicxrs");
	// opo
	task.findLPSS("ttoopo");
}
main();

Output

 Given string :  itiiiigi
 Result : iiii
 Resultant length :  4
 Given string :  vvvvivfvim
 Result : ivfvi
 Resultant length :  5
 Given string :  xyzacivicxrs
 Result : civic
 Resultant length :  5
 Given string :  ttoopo
 Result : opo
 Resultant length :  3
// Kotlin program for
// Longest palindromic substring solution
class LongestPalindrome
{
	// Returns length of palindrome in given indexes
	fun palindromeLength(text: String, l: Int, r: Int, n: Int): Int
	{
		var low: Int = l;
		var high: Int = r;
		var count: Int = 0;
		while (low >= 0 && high < n && 
               text.get(low) == text.get(high))
		{
			// When palindrome pairs exist
			count += 2;
			// Change position
			low -= 1;
			high += 1;
		}
		return count;
	}
	fun findLPSS(text: String): Unit
	{
		// Get the length of given text
		val n: Int = text.length;
		if (n == 0)
		{
			return;
		}
		// Every single character is form of palindrome.
		// So set result length is 1.
		var length: Int = 1;
		// Auxiliary variables
		var start: Int = 0;
		var a: Int ;
		var b: Int ;
		var i: Int = 1;
		// Find the max length palindrome.
		// Execute loop from 1 to n-1.
		while (i < n)
		{
			// Case A
			// Use to maintain order of left and right elements.
			// i is center point.
			// r consider right side boundary.
			// And l consider left side boundary.
			// Current boundary position
			// l = i - 1
			// r = i + 1
			a = this.palindromeLength(text, i - 1, i + 1, n) + 1;
			// Case B
			// When [i] location is not center of palindrome 
			// so test with change of boundary. 
			// Current boundary position
			// l = i - 1
			// r = i 
			b = this.palindromeLength(text, i - 1, i, n);
			if (b > a)
			{
				// Select largest
				a = b;
			}
			if (a > length)
			{
				// Change resultant length
				length = a;
				start = i - (a / 2);
			}
			i += 1;
		}
		// Display given text
		print("\n Given string : " + text);
		print("\n Result : ");
		i = start;
		// Display resultant palindrome
		while (i <= start + length - 1)
		{
			print(text.get(i));
			i += 1;
		}
		// Display length of resultant palindrome
		print("\n Resultant length : " + length);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: LongestPalindrome = LongestPalindrome();
	// Test
	// iiii
	task.findLPSS("itiiiigi");
	// ivfvi
	task.findLPSS("vvvvivfvim");
	// civic
	task.findLPSS("xyzacivicxrs");
	// opo
	task.findLPSS("ttoopo");
}

Output

 Given string : itiiiigi
 Result : iiii
 Resultant length : 4
 Given string : vvvvivfvim
 Result : ivfvi
 Resultant length : 5
 Given string : xyzacivicxrs
 Result : civic
 Resultant length : 5
 Given string : ttoopo
 Result : opo
 Resultant length : 3


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