Check if any permutation of string is palindrome
Here given code implementation process.
import java.util.HashMap;
/*
Java Program
Check if any permutation of string is palindrome
*/
public class Palindrome
{
public void isPalindromicPermutation(String text)
{
// Use to count character frequency
HashMap < Character, Integer > frequency =
new HashMap < Character, Integer > ();
boolean status = true;
boolean odd = false;
// Count frequency of given text
for (int i = 0; i < text.length(); ++i)
{
if (frequency.containsKey(text.charAt(i)))
{
// Increase character frequency
frequency.put(text.charAt(i), frequency.get(text.charAt(i)) + 1);
}
else
{
// Add new character with one frequency
frequency.put(text.charAt(i), 1);
}
}
// Check that if given string can be palindrome
for (char info: frequency.keySet())
{
if (frequency.get(info) % 2 != 0)
{
if (odd == false)
{
// Get single character
odd = true;
}
else
{
// When have more than two odd character
status = false;
break;
}
}
}
System.out.print("\n Given Text : " + text);
if (status == true)
{
// When permutation is form of palindrome
System.out.print("\n Yes ");
// Auxiliary variables
String left = "";
String right = "";
String middle = "";
int count = 0;
// Collect of palindromic sequence
for (char info: frequency.keySet())
{
count = frequency.get(info);
while (count > 0)
{
if (count == 1)
{
middle += info;
count--;
}
else
{
left = left + info;
right = info + right;
count = count - 2;
}
}
}
// Display a palindrome sequence
System.out.print(" : [" + left + middle + right + "]");
}
else
{
System.out.print("\n No ");
}
}
public static void main(String[] args)
{
Palindrome task = new Palindrome();
// Test Cases
task.isPalindromicPermutation("91002233791");
task.isPalindromicPermutation("abccbbaa");
task.isPalindromicPermutation("xyziiyyyx");
}
}
input
Given Text : 91002233791
Yes : [01239793210]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [xyyiziyyx]
// Include header file
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
/*
C++ Program
Check if any permutation of string is palindrome
*/
class Palindrome
{
public: void isPalindromicPermutation(string text)
{
// Use to count character frequency
unordered_map < char, int > frequency;
bool status = true;
bool odd = false;
// Count frequency of given text
for (int i = 0; i < text.length(); ++i)
{
if (frequency.find(text[i]) != frequency.end())
{
// Increase character frequency
frequency[text[i]] = frequency[text[i]] + 1;
}
else
{
// Add new character with one frequency
frequency[text[i]] = 1;
}
}
// Check that if given string can be palindrome
for (auto &info: frequency)
{
if (info.second % 2 != 0)
{
if (odd == false)
{
// Get single character
odd = true;
}
else
{
// When have more than two odd character
status = false;
break;
}
}
}
cout << "\n Given Text : " << text;
if (status == true)
{
// When permutation is form of palindrome
cout << "\n Yes ";
// Auxiliary variables
string left = "";
string right = "";
string middle = "";
int count = 0;
// Collect of palindromic sequence
for (auto &info: frequency)
{
count = info.second;
while (count > 0)
{
if (count == 1)
{
middle += info.first;
count--;
}
else
{
left = left + info.first;
right = info.first + right;
count = count - 2;
}
}
}
// Display a palindrome sequence
cout << " : [" << left + middle + right << "]";
}
else
{
cout << "\n No ";
}
}
};
int main()
{
Palindrome *task = new Palindrome();
// Test Cases
task->isPalindromicPermutation("91002233791");
task->isPalindromicPermutation("abccbbaa");
task->isPalindromicPermutation("xyziiyyyx");
return 0;
}
input
Given Text : 91002233791
Yes : [39102720193]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [yyixzxiyy]
// Include namespace system
using System;
using System.Collections.Generic;
/*
Csharp Program
Check if any permutation of string is palindrome
*/
public class Palindrome
{
public void isPalindromicPermutation(String text)
{
// Use to count character frequency
Dictionary < char, int > frequency = new Dictionary < char, int > ();
Boolean status = true;
Boolean odd = false;
// Count frequency of given text
for (int i = 0; i < text.Length; ++i)
{
if (frequency.ContainsKey(text[i]))
{
// Increase character frequency
frequency[text[i]] = frequency[text[i]] + 1;
}
else
{
// Add new character with one frequency
frequency.Add(text[i], 1);
}
}
// Check that if given string can be palindrome
foreach(KeyValuePair < char, int > info in frequency)
{
if (info.Value % 2 != 0)
{
if (odd == false)
{
// Get single character
odd = true;
}
else
{
// When have more than two odd character
status = false;
break;
}
}
}
Console.Write("\n Given Text : " + text);
if (status == true)
{
// When permutation is form of palindrome
Console.Write("\n Yes ");
// Auxiliary variables
String left = "";
String right = "";
String middle = "";
int count = 0;
// Collect of palindromic sequence
foreach(KeyValuePair < char, int > info in frequency)
{
count = info.Value;
while (count > 0)
{
if (count == 1)
{
middle += info.Key;
count--;
}
else
{
left = left + info.Key;
right = info.Key + right;
count = count - 2;
}
}
}
// Display a palindrome sequence
Console.Write(" : [" + left + middle + right + "]");
}
else
{
Console.Write("\n No ");
}
}
public static void Main(String[] args)
{
Palindrome task = new Palindrome();
// Test Cases
task.isPalindromicPermutation("91002233791");
task.isPalindromicPermutation("abccbbaa");
task.isPalindromicPermutation("xyziiyyyx");
}
}
input
Given Text : 91002233791
Yes : [91023732019]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [xyyiziyyx]
<?php
/*
Php Program
Check if any permutation of string is palindrome
*/
class Palindrome
{
public function isPalindromicPermutation($text)
{
// Use to count character frequency
$frequency = array();
$status = true;
$odd = false;
// Count frequency of given text
for ($i = 0; $i < strlen($text); ++$i)
{
if (array_key_exists($text[$i], $frequency))
{
// Increase character frequency
$frequency[$text[$i]] = $frequency[$text[$i]] + 1;
}
else
{
// Add new character with one frequency
$frequency[$text[$i]] = 1;
}
}
// Check that if given string can be palindrome
foreach($frequency as $key => $value)
{
if ($value % 2 != 0)
{
if ($odd == false)
{
// Get single character
$odd = true;
}
else
{
// When have more than two odd character
$status = false;
break;
}
}
}
echo("\n Given Text : ".$text);
if ($status == true)
{
// When permutation is form of palindrome
echo("\n Yes ");
// Auxiliary variables
$left = "";
$right = "";
$middle = "";
$count = 0;
// Collect of palindromic sequence
foreach($frequency as $key => $value)
{
$count = $value;
while ($count > 0)
{
if ($count == 1)
{
$middle = $middle.$key;
$count--;
}
else
{
$left = $left.$key;
$right = $key.$right;
$count = $count - 2;
}
}
}
// Display a palindrome sequence
echo(" : [".$left.$middle.$right.
"]");
}
else
{
echo("\n No ");
}
}
}
function main()
{
$task = new Palindrome();
// Test Cases
$task->isPalindromicPermutation("91002233791");
$task->isPalindromicPermutation("abccbbaa");
$task->isPalindromicPermutation("xyziiyyyx");
}
main();
input
Given Text : 91002233791
Yes : [91023732019]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [xyyiziyyx]
/*
Node JS Program
Check if any permutation of string is palindrome
*/
class Palindrome
{
isPalindromicPermutation(text)
{
// Use to count character frequency
var frequency = new Map();
var status = true;
var odd = false;
// Count frequency of given text
for (var i = 0; i < text.length; ++i)
{
if (frequency.has(text.charAt(i)))
{
// Increase character frequency
frequency.set(text.charAt(i), frequency.get(text.charAt(i)) + 1);
}
else
{
// Add new character with one frequency
frequency.set(text.charAt(i), 1);
}
}
// Check that if given string can be palindrome
for (let [key, value] of frequency)
{
if (value % 2 != 0)
{
if (odd == false)
{
// Get single character
odd = true;
}
else
{
// When have more than two odd character
status = false;
break;
}
}
}
process.stdout.write("\n Given Text : " + text);
if (status == true)
{
// When permutation is form of palindrome
process.stdout.write("\n Yes ");
// Auxiliary variables
var left = "";
var right = "";
var middle = "";
var count = 0;
// Collect of palindromic sequence
for (let [key, value] of frequency)
{
count = value;
while (count > 0)
{
if (count == 1)
{
middle += key;
count--;
}
else
{
left = left + key;
right = key + right;
count = count - 2;
}
}
}
// Display a palindrome sequence
process.stdout.write(" : [" + left + middle + right + "]");
}
else
{
process.stdout.write("\n No ");
}
}
}
function main()
{
var task = new Palindrome();
// Test Cases
task.isPalindromicPermutation("91002233791");
task.isPalindromicPermutation("abccbbaa");
task.isPalindromicPermutation("xyziiyyyx");
}
main();
input
Given Text : 91002233791
Yes : [91023732019]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [xyyiziyyx]
# Python 3 Program
# Check if any permutation of string is palindrome
class Palindrome :
def isPalindromicPermutation(self, text) :
# Use to count character frequency
frequency = dict()
status = True
odd = False
# Count frequency of given text
i = 0
while (i < len(text)) :
if (text[i] in frequency.keys()) :
# Increase character frequency
frequency[text[i]] = frequency.get(text[i]) + 1
else :
# Add new character with one frequency
frequency[text[i]] = 1
i += 1
# Check that if given string can be palindrome
for key, value in frequency.items() :
if (value % 2 != 0) :
if (odd == False) :
# Get single character
odd = True
else :
# When have more than two odd character
status = False
break
print("\n Given Text : ", text, end = "")
if (status == True) :
# When permutation is form of palindrome
print("\n Yes ", end = "")
# Auxiliary variables
left = ""
right = ""
middle = ""
count = 0
# Collect of palindromic sequence
for key, value in frequency.items() :
count = value
while (count > 0) :
if (count == 1) :
middle += key
count -= 1
else :
left = left + key
right = key + right
count = count - 2
# Display a palindrome sequence
print(" : [", left + middle + right ,"]", end = "")
else :
print("\n No ", end = "")
def main() :
task = Palindrome()
# Test Cases
task.isPalindromicPermutation("91002233791")
task.isPalindromicPermutation("abccbbaa")
task.isPalindromicPermutation("xyziiyyyx")
if __name__ == "__main__": main()
input
Given Text : 91002233791
Yes : [ 09132723190 ]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [ xiyyzyyix ]
# Ruby Program
# Check if any permutation of string is palindrome
class Palindrome
def isPalindromicPermutation(text)
# Use to count character frequency
frequency = Hash.new()
status = true
odd = false
# Count frequency of given text
i = 0
while (i < text.length)
if (frequency.key?(text[i]))
# Increase character frequency
frequency[text[i]] = frequency[text[i]] + 1
else
# Add new character with one frequency
frequency[text[i]] = 1
end
i += 1
end
# Check that if given string can be palindrome
frequency.each { | key, value |
if (value % 2 != 0)
if (odd == false)
# Get single character
odd = true
else
# When have more than two odd character
status = false
break
end
end
}
print("\n Given Text : ", text)
if (status == true)
# When permutation is form of palindrome
print("\n Yes ")
# Auxiliary variables
left = ""
right = ""
middle = ""
count = 0
# Collect of palindromic sequence
frequency.each { | key, value |
count = value
while (count > 0)
if (count == 1)
middle += key
count -= 1
else
left = left + key
right = key + right
count = count - 2
end
end
}
# Display a palindrome sequence
print(" : [", left + middle + right ,"]")
else
print("\n No ")
end
end
end
def main()
task = Palindrome.new()
# Test Cases
task.isPalindromicPermutation("91002233791")
task.isPalindromicPermutation("abccbbaa")
task.isPalindromicPermutation("xyziiyyyx")
end
main()
input
Given Text : 91002233791
Yes : [91023732019]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [xyyiziyyx]
import scala.collection.mutable._;
import scala.util.control.Breaks._;
/*
Scala Program
Check if any permutation of string is palindrome
*/
class Palindrome()
{
def isPalindromicPermutation(text: String): Unit = {
// Use to count character frequency
var frequency: HashMap[Character, Int] =
new HashMap[Character, Int]();
var status: Boolean = true;
var odd: Boolean = false;
// Count frequency of given text
var i: Int = 0;
while (i < text.length())
{
if (frequency.contains(text.charAt(i)))
{
// Increase character frequency
frequency.addOne(text.charAt(i),
frequency.get(text.charAt(i)).get + 1);
}
else
{
// Add new character with one frequency
frequency.addOne(text.charAt(i), 1);
}
i += 1;
}
breakable
{
// Check that if given string can be palindrome
for ((key, value) <- frequency)
{
if (value % 2 != 0)
{
if (odd == false)
{
// Get single character
odd = true;
}
else
{
// When have more than two odd character
status = false;
break;
}
}
}
}
print("\n Given Text : " + text);
if (status == true)
{
// When permutation is form of palindrome
print("\n Yes ");
// Auxiliary variables
var left: String = "";
var right: String = "";
var middle: String = "";
var count: Int = 0;
// Collect of palindromic sequence
for ((key, value) <-frequency)
{
count = value;
while (count > 0)
{
if (count == 1)
{
middle += key;
count -= 1;
}
else
{
left = left + key;
right = ""+key + right;
count = count - 2;
}
}
}
// Display a palindrome sequence
print(" : [" + left + middle + right + "]");
}
else
{
print("\n No ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Palindrome = new Palindrome();
// Test Cases
task.isPalindromicPermutation("91002233791");
task.isPalindromicPermutation("abccbbaa");
task.isPalindromicPermutation("xyziiyyyx");
}
}
input
Given Text : 91002233791
Yes : [01239793210]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [xiyyzyyix]
import Foundation;
/*
Swift 4 Program
Check if any permutation of string is palindrome
*/
class Palindrome
{
func isPalindromicPermutation(_ t: String)
{
let text = Array(t);
// Use to count character frequency
var frequency = [Character : Int]();
var status = true;
var odd = false;
// Count frequency of given text
var i = 0;
while (i < text.count)
{
if (frequency.keys.contains(text[i]))
{
// Increase character frequency
frequency[text[i]] = frequency[text[i]]! + 1;
}
else
{
// Add new character with one frequency
frequency[text[i]] = 1;
}
i += 1;
}
// Check that if given string can be palindrome
for (_, value) in frequency
{
if (value % 2 != 0)
{
if (odd == false)
{
// Get single character
odd = true;
}
else
{
// When have more than two odd character
status = false;
break;
}
}
}
print("\n Given Text : ", t, terminator: "");
if (status == true)
{
// When permutation is form of palindrome
print("\n Yes ", terminator: "");
// Auxiliary variables
var left = "";
var right = "";
var middle = "";
var count = 0;
// Collect of palindromic sequence
for (key, value) in frequency
{
count = value;
while (count > 0)
{
if (count == 1)
{
middle += String(key);
count -= 1;
}
else
{
left = left + String(key);
right = String(key) + right;
count = count - 2;
}
}
}
// Display a palindrome sequence
print(" : [", left + middle + right ,"]", terminator: "");
}
else
{
print("\n No ", terminator: "");
}
}
}
func main()
{
let task = Palindrome();
// Test Cases
task.isPalindromicPermutation("91002233791");
task.isPalindromicPermutation("abccbbaa");
task.isPalindromicPermutation("xyziiyyyx");
}
main();
input
Given Text : 91002233791
Yes : [ 12390709321 ]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [ xyyiziyyx ]
/*
Kotlin Program
Check if any permutation of string is palindrome
*/
class Palindrome
{
fun isPalindromicPermutation(text: String): Unit
{
// Use to count character frequency
val frequency = mutableMapOf < Char , Int > ();
var status: Boolean = true;
var odd: Boolean = false;
// Count frequency of given text
var i: Int = 0;
while (i < text.length)
{
if (frequency.containsKey(text.get(i)))
{
// Increase character frequency
frequency.put(text.get(i), frequency.getValue(text.get(i)) + 1);
}
else
{
// Add new character with one frequency
frequency.put(text.get(i), 1);
}
i += 1;
}
// Check that if given string can be palindrome
for ((_, value) in frequency)
{
if (value % 2 != 0)
{
if (odd == false)
{
// Get single character
odd = true;
}
else
{
// When have more than two odd character
status = false;
break;
}
}
}
print("\n Given Text : " + text);
if (status == true)
{
// When permutation is form of palindrome
print("\n Yes ");
// Auxiliary variables
var left: String = "";
var right: String = "";
var middle: String = "";
var count: Int ;
// Collect of palindromic sequence
for ((key,value) in frequency)
{
count = value;
while (count > 0)
{
if (count == 1)
{
middle += key;
count -= 1;
}
else
{
left = left + key.toString();
right = key.toString() + right;
count = count - 2;
}
}
}
// Display a palindrome sequence
print(" : [" + left + middle + right + "]");
}
else
{
print("\n No ");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Palindrome = Palindrome();
// Test Cases
task.isPalindromicPermutation("91002233791");
task.isPalindromicPermutation("abccbbaa");
task.isPalindromicPermutation("xyziiyyyx");
}
input
Given Text : 91002233791
Yes : [91023732019]
Given Text : abccbbaa
No
Given Text : xyziiyyyx
Yes : [xyyiziyyx]
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