Maximize the minimum length of K palindromic Strings formed from given String
Given a string str of length N, and an integer K, the task is to form K different strings by choosing characters from the given string such that all the strings formed are palindrome and the length of the smallest string among the K strings is maximum possible.
Examples:
Input: str = “qrsprtps”, K = 2
Output: 3
Explanation: The 2 strings are: “pqp” and ” rssr”.
Using 2 ‘p’ and 1 ‘q’ the 1st string is formed and using 2 ‘r’ and 2 ‘s’ the 2nd string is formed.
The 1st string is the smallest among the K strings and is of length 3 so the answer is 3.
Input: str = “aaaabcbabca”, K = 3
Output: 3
Explanation: Possible 3 palindromic strings of maximum possible length are: “aba”, “aba”, “aba”.
The length of the smallest string among these is 3.
Approach: The approach is based on the Greedy technique. Try to distribute a pair of same characters to K strings equally. Make pairs of identical characters to ensure the string formed will be a palindrome. An even length palindrome of length N will have N/2 such pairs and an odd length will have an extra character along with the N/2 pairs.
- Count the frequency of the characters in the given string and the number of pairs that can be formed with those characters.
- Distribute these pairs among K strings as long as there are K pairs available. (i.e. if there are 5 such pairs and K = 2 then, distribute 2 pairs to each so that a 4 length palindromic string can be formed single pair will be left undistributed)
- Now there will be all even length palindromic strings. As the pairs left cannot be distributed among all the strings so the smallest string will have a length of twice the number of character pairs distributed to each of the strings.
- Try to add 1 more character to see if an odd length string can be formed.
- From the remaining characters i.e., the characters which were not a part of the pairs and the characters from the leftover pair of characters, add 1 character to each string to increase the maximum length by 1.
- If there are at least K such characters present then only the minimum length can be increased by one for all strings.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int form_K_Strings(string& str, int k)
{
int n = str.size();
unordered_map< char , int > freq;
for ( auto i : str)
freq[i]++;
int pairs = 0, remChar = 0;
for ( auto i : freq) {
pairs += (i.second / 2);
if (i.second % 2 == 1)
remChar++;
}
int distributed = pairs / k;
int res = distributed * 2;
int remPairs = pairs % k;
remChar += 2 * remPairs;
if (remChar >= k)
res++;
return res;
}
int main()
{
string str = "qrsprtps" ;
int K = 2;
cout << form_K_Strings(str, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int form_K_Strings(String str, int k)
{
int n = str.length();
HashMap<Character, Integer> freq = new HashMap<>();
char [] strArray = str.toCharArray();
for ( char c : strArray) {
if (freq.containsKey(c)) {
freq.put(c, freq.get(c) + 1 );
}
else {
freq.put(c, 1 );
}
}
int pairs = 0 , remChar = 0 ;
for (Map.Entry<Character, Integer> i :
freq.entrySet()) {
pairs += (i.getValue() / 2 );
if (i.getValue() % 2 == 1 )
remChar++;
}
int distributed = pairs / k;
int res = distributed * 2 ;
int remPairs = pairs % k;
remChar += 2 * remPairs;
if (remChar >= k)
res++;
return res;
}
public static void main(String[] args)
{
String str = "qrsprtps" ;
int K = 2 ;
System.out.print(form_K_Strings(str, K));
}
}
|
Python3
from collections import defaultdict
def form_K_Strings(st, k):
n = len (st)
freq = defaultdict( int )
for i in st:
freq[i] + = 1
pairs = 0
remChar = 0
for i in freq:
pairs + = (freq[i] / / 2 )
if (freq[i] % 2 = = 1 ):
remChar + = 1
distributed = pairs / / k
res = distributed * 2
remPairs = pairs % k
remChar + = 2 * remPairs
if (remChar > = k):
res + = 1
return res
if __name__ = = "__main__" :
st = "qrsprtps"
K = 2
print (form_K_Strings(st, K))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int form_K_Strings( string str, int k)
{
int n = str.Length;
Dictionary< char , int > freq =
new Dictionary< char , int >();
char [] strArray = str.ToCharArray();
foreach ( char c in strArray) {
if (!freq.ContainsKey(c)) {
freq.Add(c, 1);
}
else {
freq += 1;
}
}
int pairs = 0, remChar = 0;
foreach (KeyValuePair< char , int > i in freq)
{
pairs += (i.Value / 2);
if (i.Value % 2 == 1)
remChar++;
}
int distributed = pairs / k;
int res = distributed * 2;
int remPairs = pairs % k;
remChar += 2 * remPairs;
if (remChar >= k)
res++;
return res;
}
public static void Main()
{
string str = "qrsprtps" ;
int K = 2;
Console.Write(form_K_Strings(str, K));
}
}
|
Javascript
<script>
function form_K_Strings(str, k)
{
let n = str.length;
let freq = new Map();
for (let i = 0; i < str.length; i++) {
if (freq.has(str[i])) {
freq.set(str[i], freq.get(str[i]) + 1)
}
else {
freq.set(str[i], 1)
}
}
let pairs = 0, remChar = 0;
for (let [key, val] of freq) {
pairs += Math.floor(val / 2);
if (val % 2 == 1)
remChar++;
}
let distributed = Math.floor(pairs / k);
let res = distributed * 2;
let remPairs = pairs % k;
remChar += 2 * remPairs;
if (remChar >= k)
res++;
return res;
}
let str = "qrsprtps" ;
let K = 2;
document.write(form_K_Strings(str, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
01 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...