Check if K palindromic strings can be formed from a given string
Last Updated :
06 Jan, 2023
Given a string S of size N and an integer K, the task is to find whether the characters of the string can be arranged to make K palindromic strings simultaneously.
Examples:
Input: S = “annabelle”, K = 2
Output: Yes
Explanation:
All characters of string S can be distributed into “elble” and “anna” which are both palindromic.
Input: S =”abcd”, K = 4
Output: Yes
Explanation:
Partition all characters of string as single character.
Approach
- If the frequency of every character is even and K lies between 1 and N then it is always possible to form K palindrome strings.
- But if there are some characters (say odd_count) with odd frequency, then K must lie between odd_count and N for K palindromic strings to be possible.
Hence, follow the steps below to solve the problem:
If K exceeds the length of the string, straightaway print “No”.
- Store the frequency of all characters in a Map.
- Count the number of characters having an odd frequency.
- If the count is less than given K, then print “No”. Otherwise, print “Yes”.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <map>
using namespace std;
bool can_Construct(string S, int K)
{
map< char , int > m;
int i = 0, j = 0, p = 0;
if (S.length() == K) {
return true ;
}
map< char , int >::iterator h;
for (i = 0; i < S.length(); i++) {
m[S[i]] = m[S[i]] + 1;
}
if (K > S.length()) {
return false ;
}
else {
for (h = m.begin(); h != m.end(); h++) {
if (m[h->first] % 2 != 0) {
p = p + 1;
}
}
}
if (K < p) {
return false ;
}
return true ;
}
int main()
{
string S = "annabelle" ;
int K = 4;
if (can_Construct(S, K)) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
|
Java
import java.util.*;
class GFG{
static boolean can_Construct(String S, int K)
{
Map<Character, Integer> m = new HashMap<>();
int p = 0 ;
if (S.length() == K)
return true ;
for ( int i = 0 ; i < S.length(); i++)
m.put(S.charAt(i),
m.getOrDefault(S.charAt(i), 0 ) + 1 );
if (K > S.length())
return false ;
else
{
for (Integer h : m.values())
{
if (h % 2 != 0 )
p = p + 1 ;
}
}
if (K < p)
return false ;
return true ;
}
public static void main (String[] args)
{
String S = "annabelle" ;
int K = 4 ;
if (can_Construct(S, K))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def can_Construct(S, K):
m = dict ()
p = 0
if ( len (S) = = K):
return True
for i in S:
m[i] = m.get(i, 0 ) + 1
if (K > len (S)):
return False
else :
for h in m:
if (m[h] % 2 ! = 0 ):
p = p + 1
if (K < p):
return False
return True
if __name__ = = '__main__' :
S = "annabelle"
K = 4
if (can_Construct(S, K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool can_Construct(String S,
int K)
{
Dictionary< char ,
int > m = new Dictionary< char ,
int >();
int p = 0;
if (S.Length == K)
return true ;
for ( int i = 0; i < S.Length; i++)
if (!m.ContainsKey(S[i]))
m.Add(S[i], 1);
else
m[S[i]] = m[S[i]] + 1;
if (K > S.Length)
return false ;
else
{
foreach ( int h in m.Values)
{
if (h % 2 != 0)
p = p + 1;
}
}
if (K < p)
return false ;
return true ;
}
public static void Main(String[] args)
{
String S = "annabelle" ;
int K = 4;
if (can_Construct(S, K))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function can_Construct(S, K)
{
var m = new Map();
var i = 0, j = 0, p = 0;
if (S.length == K) {
return true ;
}
for (i = 0; i < S.length; i++) {
if (m.has(S[i]))
m.set(S[i], m.get(S[i])+1)
else
m.set(S[i], 1)
}
if (K > S.length) {
return false ;
}
else {
m.forEach((value, key) => {
if (value%2 != 0) {
p = p + 1;
}
});
}
if (K < p) {
return false ;
}
return true ;
}
var S = "annabelle" ;
var K = 4;
if (can_Construct(S, K)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
|
Time Complexity: O (N).
Auxiliary Space: O (N).
Share your thoughts in the comments
Please Login to comment...