Check if K palindromic strings can be formed from a given string

Here given code implementation process.

// Java Program 
// Check if K palindromic strings can be formed from a given string
import java.util.HashMap;

public class Palindrome
{
    // Check whether K palindrome can be generated in character 
    // if rearrangement is allowed in the character
    public boolean constructKPalindrome(String text, int k)
    {

        if( k == text.length()) 
        { 
            // When k is equal to text length
            // Because each character are generate a palindrome
            return true;
        }
        else if(k > text.length())
        {
            // When K is higher of  text length
            return false;
        }
        else
        {
            // Define some auxiliary variables
            int i = 0;
            int count = 0;
            
            // Use to count character frequency
            HashMap < Character, Integer > record = new HashMap < Character, Integer > ();

            // iterate the loop through by length
            // And count character frequency
            for (i = 0; i < text.length(); i++)
            {
           
                if (record.containsKey(text.charAt(i)))
                {
                    record.put(text.charAt(i), record.get(text.charAt(i)) + 1);
                }
                else
                {
                    record.put(text.charAt(i), 1);
                }
            }
            for (int value: record.values())
            {
                if (value % 2 != 0)
                {
                    // When frequency is odd
                   count++;
                }
            }
            if(count > k)
            {
                // When odd frequency character is greater than given K
                return false;
            }
            else 
            {
                return true;
            }
      
        }
    }
    public static void main(String[] args)
    {
        Palindrome task = new Palindrome();

        String text = "aacccaaf";
        // possible palindrome
        int k = 3;
        // text = aacccaaf
        // 1) aa  
        // 2) ccc
        // 3) afa 
      
        System.out.print(" Given text : "+ text);
        System.out.print("\n Given K    : "+ k);

        if(task.constructKPalindrome(text,k))
        {
            System.out.print("\n Result : Possible\n");
        }
        else
        {
            System.out.print("\n Result : Not Possible\n");
        }
    }
}

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible
// Include header file
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
// C++ Program
// Check if K palindromic strings can be formed from a given string
class Palindrome
{
	public:
		// Check whether K palindrome can be generated in character
		// if rearrangement is allowed in the character
		bool constructKPalindrome(string text, int k)
		{
			if (k == text.length())
			{
				// When k is equal to text length
				// Because each character are generate a palindrome
				return true;
			}
			else if (k > text.length())
			{
				// When K is higher of  text length
				return false;
			}
			else
			{
				// Define some auxiliary variables
				int i = 0;
				int count = 0;
				// Use to count character frequency
				unordered_map < char, int > record;
				// iterate the loop through by length
				// And count character frequency
				for (i = 0; i < text.length(); i++)
				{
					if (record.find(text[i]) != record.end())
					{
						record[text[i]] = record[text[i]] + 1;
					}
					else
					{
						record[text[i]] = 1;
					}
				}
				for (auto &value: record)
				{
					if (value.first % 2 != 0)
					{
						// When frequency is odd
						count++;
					}
				}
				if (count > k)
				{
					// When odd frequency character is greater than given K
					return false;
				}
				else
				{
					return true;
				}
			}
		}
};
int main()
{
	Palindrome task = Palindrome();
	string text = "aacccaaf";
	// possible palindrome
	int k = 3;
	// text = aacccaaf
	// 1) aa
	// 2) ccc
	// 3) afa
	cout << " Given text : " << text;
	cout << "\n Given K    : " << k;
	if (task.constructKPalindrome(text, k))
	{
		cout << "\n Result : Possible\n";
	}
	else
	{
		cout << "\n Result : Not Possible\n";
	}
	return 0;
}

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Check if K palindromic strings can be formed from a given string
public class Palindrome
{
	// Check whether K palindrome can be generated in character
	// if rearrangement is allowed in the character
	public Boolean constructKPalindrome(String text, int k)
	{
		if (k == text.Length)
		{
			// When k is equal to text length
			// Because each character are generate a palindrome
			return true;
		}
		else if (k > text.Length)
		{
			// When K is higher of  text length
			return false;
		}
		else
		{
			// Define some auxiliary variables
			int i = 0;
			int count = 0;
			// Use to count character frequency
			Dictionary < char, int > record = new Dictionary < char, int > ();
			// iterate the loop through by length
			// And count character frequency
			for (i = 0; i < text.Length; i++)
			{
				if (record.ContainsKey(text[i]))
				{
					record[text[i]] = record[text[i]] + 1;
				}
				else
				{
					record.Add(text[i], 1);
				}
			}
			foreach(KeyValuePair < char, int > info in record)
			{
				if (info.Value % 2 != 0)
				{
					// When frequency is odd
					count++;
				}
			}
			if (count > k)
			{
				// When odd frequency character is greater than given K
				return false;
			}
			else
			{
				return true;
			}
		}
	}
	public static void Main(String[] args)
	{
		Palindrome task = new Palindrome();
		String text = "aacccaaf";
		// possible palindrome
		int k = 3;
		// text = aacccaaf
		// 1) aa
		// 2) ccc
		// 3) afa
		Console.Write(" Given text : " + text);
		Console.Write("\n Given K    : " + k);
		if (task.constructKPalindrome(text, k))
		{
			Console.Write("\n Result : Possible\n");
		}
		else
		{
			Console.Write("\n Result : Not Possible\n");
		}
	}
}

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible
<?php
// Php Program
// Check if K palindromic strings can be formed from a given string
class Palindrome
{
	// Check whether K palindrome can be generated in character
	// if rearrangement is allowed in the character
	public	function constructKPalindrome($text, $k)
	{
		if ($k == strlen($text))
		{
			// When k is equal to text length
			// Because each character are generate a palindrome
			return true;
		}
		else if ($k > strlen($text))
		{
			// When K is higher of  text length
			return false;
		}
		else
		{
			// Define some auxiliary variables
			$i = 0;
			$count = 0;
			// Use to count character frequency
			$record = array();
			// iterate the loop through by length
			// And count character frequency
			for ($i = 0; $i < strlen($text); $i++)
			{
				if (array_key_exists($text[$i], $record))
				{
					$record[$text[$i]] = $record[$text[$i]] + 1;
				}
				else
				{
					$record[$text[$i]] = 1;
				}
			}
			foreach($record as $key => $value)
			{
				if ($value % 2 != 0)
				{
					// When frequency is odd
					$count++;
				}
			}
			if ($count > $k)
			{
				// When odd frequency character is greater than given K
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}

function main()
{
	$task = new Palindrome();
	$text = "aacccaaf";
	// possible palindrome
	$k = 3;
	// text = aacccaaf
	// 1) aa
	// 2) ccc
	// 3) afa
	echo " Given text : ". $text;
	echo "\n Given K    : ". $k;
	if ($task->constructKPalindrome($text, $k))
	{
		echo "\n Result : Possible\n";
	}
	else
	{
		echo "\n Result : Not Possible\n";
	}
}
main();

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible
// Node Js Program
// Check if K palindromic strings can be formed from a given string
class Palindrome
{
	// Check whether K palindrome can be generated in character
	// if rearrangement is allowed in the character
	constructKPalindrome(text, k)
	{
		if (k == text.length)
		{
			// When k is equal to text length
			// Because each character are generate a palindrome
			return true;
		}
		else if (k > text.length)
		{
			// When K is higher of  text length
			return false;
		}
		else
		{
			// Define some auxiliary variables
			var i = 0;
			var count = 0;
			// Use to count character frequency
			var record = new Map();
			// iterate the loop through by length
			// And count character frequency
			for (i = 0; i < text.length; i++)
			{
				if (record.has(text.charAt(i)))
				{
					record.set(text.charAt(i), record.get(text.charAt(i)) + 1);
				}
				else
				{
					record.set(text.charAt(i), 1);
				}
			}
			for (let [key, value] of record)
			{
				if (value % 2 != 0)
				{
					// When frequency is odd
					count++;
				}
			}
			if (count > k)
			{
				// When odd frequency character is greater than given K
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}

function main()
{
	var task = new Palindrome();
	var text = "aacccaaf";
	// possible palindrome
	var k = 3;
	// text = aacccaaf
	// 1) aa
	// 2) ccc
	// 3) afa
	process.stdout.write(" Given text : " + text);
	process.stdout.write("\n Given K    : " + k);
	if (task.constructKPalindrome(text, k))
	{
		process.stdout.write("\n Result : Possible\n");
	}
	else
	{
		process.stdout.write("\n Result : Not Possible\n");
	}
}
main();

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible
#  Python 3 Program
#  Check if K palindromic strings can be formed from a given string
class Palindrome :
	#  Check whether K palindrome can be generated in character
	#  if rearrangement is allowed in the character
	def constructKPalindrome(self, text, k) :
		if (k == len(text)) :
			#  When k is equal to text length
			#  Because each character are generate a palindrome
			return True
		elif(k > len(text)) :
			#  When K is higher of  text length
			return False
		else :
			#  Define some auxiliary variables
			i = 0
			count = 0
			#  Use to count character frequency
			record = dict()
			#  iterate the loop through by length
			#  And count character frequency
			while (i < len(text)) :
				if (text[i] in record.keys()) :
					record[text[i]] = record.get(text[i]) + 1
				else :
					record[text[i]] = 1
				i += 1
			
			for key, value in record.items() :
				if (value % 2 != 0) :
					#  When frequency is odd
					count += 1
				
			
			if (count > k) :
				#  When odd frequency character is greater than given K
				return False
			else :
				return True
			

def main() :
	task = Palindrome()
	text = "aacccaaf"
	#  possible palindrome
	k = 3
	#  text = aacccaaf
	#  1) aa
	#  2) ccc
	#  3) afa
	print(" Given text : ", text, end = "")
	print("\n Given K    : ", k, end = "")
	if (task.constructKPalindrome(text, k)) :
		print("\n Result : Possible")
	else :
		print("\n Result : Not Possible")
	

if __name__ == "__main__": main()

Output

 Given text :  aacccaaf
 Given K    :  3
 Result : Possible
#  Ruby Program
#  Check if K palindromic strings can be formed from a given string
class Palindrome 
	#  Check whether K palindrome can be generated in character
	#  if rearrangement is allowed in the character
	def constructKPalindrome(text, k) 
		if (k == text.length) 
			#  When k is equal to text length
			#  Because each character are generate a palindrome
			return true
		elsif(k > text.length) 
			#  When K is higher of  text length
			return false
		else 
			#  Define some auxiliary variables
			i = 0
			count = 0
			#  Use to count character frequency
			record = Hash.new
			#  iterate the loop through by length
			#  And count character frequency
			while (i < text.length) 
				if (record.key?(text[i])) 
					record[text[i]] = record[text[i]] + 1
				else 
					record[text[i]] = 1
				end

				i += 1
			end

			record.each { | key, value | 
				if (value % 2 != 0) 
					#  When frequency is odd
					count += 1
				end
			}
			if (count > k) 
				#  When odd frequency character is greater than given K
				return false
			else 
				return true
			end

		end

	end

end

def main() 
	task = Palindrome.new()
	text = "aacccaaf"
	#  possible palindrome
	k = 3
	#  text = aacccaaf
	#  1) aa
	#  2) ccc
	#  3) afa
	print(" Given text : ", text)
	print("\n Given K    : ", k)
	if (task.constructKPalindrome(text, k)) 
		print("\n Result : Possible\n")
	else 
		print("\n Result : Not Possible\n")
	end

end

main()

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible
import scala.collection.mutable._;
// Scala Program
// Check if K palindromic strings can be formed from a given string
class Palindrome
{
	// Check whether K palindrome can be generated in character
	// if rearrangement is allowed in the character
	def constructKPalindrome(text: String, k: Int): Boolean = {
		if (k == text.length())
		{
			// When k is equal to text length
			// Because each character are generate a palindrome
			return true;
		}
		else if (k > text.length())
		{
			// When K is higher of  text length
			return false;
		}
		else
		{
			// Define some auxiliary variables
			var i: Int = 0;
			var count: Int = 0;
			// Use to count character frequency
			var record = Map[Char, Int]();
			// iterate the loop through by length
			// And count character frequency
			while (i < text.length())
			{
				if (record.contains(text.charAt(i)))
				{
					record.addOne(text.charAt(i), 
                                  record.get(text.charAt(i)).get + 1);
				}
				else
				{
					record.addOne(text.charAt(i), 1);
				}
				i += 1;
			}
			for ((key, v) <-  record)
			{
				if (v % 2 != 0)
				{
					// When frequency is odd
					count += 1;
				}
			}
			if (count > k)
			{
				// When odd frequency character is greater than given K
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Palindrome = new Palindrome();
		var text: String = "aacccaaf";
		// possible palindrome
		var k: Int = 3;
		// text = aacccaaf
		// 1) aa
		// 2) ccc
		// 3) afa
		print(" Given text : " + text);
		print("\n Given K    : " + k);
		if (task.constructKPalindrome(text, k))
		{
			print("\n Result : Possible\n");
		}
		else
		{
			print("\n Result : Not Possible\n");
		}
	}
}

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible
import Foundation
// Swift 4 Program
// Check if K palindromic strings can be formed from a given string
class Palindrome
{
	// Check whether K palindrome can be generated in character
	// if rearrangement is allowed in the character
	func constructKPalindrome(_ t: String, _ k: Int)->Bool
	{
      	var text = Array(t);
		if (k == text.count)
		{
			// When k is equal to text length
			// Because each character are generate a palindrome
			return true;
		}
		else if (k > text.count)
		{
			// When K is higher of  text length
			return false;
		}
		else
		{
			// Define some auxiliary variables
			var i: Int = 0;
			var count: Int = 0;
			// Use to count character frequency
			var record = [Character: Int]();
			// iterate the loop through by length
			// And count character frequency
			while (i < text.count)
			{
				if (record.keys.contains(text[i]))
				{
					record[text[i]] = record[text[i]]! + 1;
				}
				else
				{
					record[text[i]] = 1;
				}
				i += 1;
			}
			for (_, value) in record
			{
				if (value % 2  != 0)
				{
					// When frequency is odd
					count += 1;
				}
			}
			if (count > k)
			{
				// When odd frequency character is greater than given K
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}
func main()
{
	let task: Palindrome = Palindrome();
	let text: String = "aacccaaf";
	// possible palindrome
	let k: Int = 3;
	// text = aacccaaf
	// 1) aa
	// 2) ccc
	// 3) afa
	print(" Given text : ", text, terminator: "");
	print("\n Given K    : ", k, terminator: "");
	if (task.constructKPalindrome(text, k))
	{
		print("\n Result : Possible");
	}
	else
	{
		print("\n Result : Not Possible");
	}
}
main();

Output

 Given text :  aacccaaf
 Given K    :  3
 Result : Possible
// Kotlin Program
// Check if K palindromic strings can be formed from a given string
class Palindrome
{
	// Check whether K palindrome can be generated in character
	// if rearrangement is allowed in the character
	fun constructKPalindrome(text: String, k: Int): Boolean
	{
		if (k == text.length)
		{
			// When k is equal to text length
			// Because each character are generate a palindrome
			return true;
		}
		else if (k > text.length)
		{
			// When K is higher of  text length
			return false;
		}
		else
		{
			// Define some auxiliary variables
			var i: Int = 0;
			var count: Int = 0;
			// Use to count character frequency
			var record = mutableMapOf < Char , Int > ();
			// iterate the loop through by length
			// And count character frequency
			while (i < text.length)
			{
				if (record.containsKey(text.get(i)))
				{
					record.put(text.get(i), record.getValue(text.get(i)) + 1);
				}
				else
				{
					record.put(text.get(i), 1);
				}
				i += 1;
			}
			for (value in record.values)
			{
				if (value % 2 != 0)
				{
					// When frequency is odd
					count += 1;
				}
			}
			if (count > k)
			{
				// When odd frequency character is greater than given K
				return false;
			}
			else
			{
				return true;
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Palindrome = Palindrome();
	var text: String = "aacccaaf";
	// possible palindrome
	var k: Int = 3;
	// text = aacccaaf
	// 1) aa
	// 2) ccc
	// 3) afa
	print(" Given text : " + text);
	print("\n Given K    : " + k);
	if (task.constructKPalindrome(text, k))
	{
		print("\n Result : Possible\n");
	}
	else
	{
		print("\n Result : Not Possible\n");
	}
}

Output

 Given text : aacccaaf
 Given K    : 3
 Result : Possible


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