Posted on by Kalkicode
Code Hash

# 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)
{

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);

{
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()
{
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;
{
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
{
}
}
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)
{
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);
{
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()
{
\$text = "aacccaaf";
// possible palindrome
\$k = 3;
// text = aacccaaf
// 1) aa
// 2) ccc
// 3) afa
echo " Given text : ". \$text;
echo "\n Given K    : ". \$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 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);
{
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() :
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 = "")
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()
text = "aacccaaf"
#  possible palindrome
k = 3
#  text = aacccaaf
#  1) aa
#  2) ccc
#  3) afa
print(" Given text : ", text)
print("\n Given K    : ", 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.get(text.charAt(i)).get + 1);
}
else
{
}
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);
{
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 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: "");
{
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 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);
{
print("\n Result : Possible\n");
}
else
{
print("\n Result : Not Possible\n");
}
}``````

#### Output

`````` Given text : aacccaaf
Given K    : 3
Result : Possible``````

## 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.

Categories
Relative Post