Count of unique palindromic strings of length X from given string
Given a string s and an integer X, our task is to find the number of distinct palindromic strings of length X from the given string.
Examples:
Input: s = “aaa”, X = 2
Output: 1
Explanation:
Here all the characters of the string is the same so we can only make a one different string {aa} of length 2.
Input: s = “aabaabbc”, X = 3
Output: 6
Explanation:
To make a palindromic string of length 3 we have 6 possible strings aaa, aba, aca, bab, bcb, bbb.
Naive Approach: The naive method is to generate all the possible subsequence of length X, then check if that subsequence forms a palindrome or not.
Time Complexity: O(2N)
Auxiliary Space: O(X)
Efficient Approach: The idea is to find the frequency of all the characters of the string. Count the different numbers of the characters we can put at the particular position and decrease the number of count by 2 because for palindromic string the position (i) and (X – i) will be the same. If the length of X is odd then we have to put the only single char at that position X/2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long findways(string s, int x)
{
if (x > ( int )s.length())
return 0;
long long int n = ( int )s.length();
int freq[26];
memset (freq, 0, sizeof freq);
for ( int i = 0; i < n; ++i)
freq[s[i] - 'a' ]++;
multiset< int > se;
for ( int i = 0; i < 26; ++i)
if (freq[i] > 0)
se.insert(freq[i]);
long long ans = 1;
for ( int i = 0; i < x / 2; ++i) {
long long int count = 0;
for ( auto u : se) {
if (u >= 2)
count++;
}
if (count == 0)
return 0;
else
ans = ans * count;
auto p = se.end();
p--;
int val = *p;
se.erase(p);
if (val > 2)
se.insert(val - 2);
}
if (x % 2 != 0) {
long long int count = 0;
for ( auto u : se)
if (u > 0)
count++;
ans = ans * count;
}
return ans;
}
int main()
{
string s = "aaa" ;
int x = 2;
cout << findways(s, x);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
class GFG{
static int findways(String s, int x)
{
if (x > s.length())
return 0 ;
int n = s.length();
int [] freq = new int [ 26 ];
Arrays.fill(freq, 0 );
for ( int i = 0 ; i < n; ++i)
freq[s.charAt(i) - 'a' ]++;
Set<Integer> se = new HashSet<>();
for ( int i = 0 ; i < 26 ; ++i)
if (freq[i] > 0 )
se.add(freq[i]);
int ans = 1 ;
for ( int i = 0 ; i < x / 2 ; ++i)
{
int count = 0 ;
for ( int u : se)
{
if (u >= 2 )
count++;
}
if (count == 0 )
return 0 ;
else
ans = ans * count;
int p = ( int )se.toArray()[se.size() - 1 ];
int val = p;
se.remove(p);
if (val > 2 )
se.add(val - 2 );
}
if (x % 2 != 0 )
{
int count = 0 ;
for ( int u : se)
if (u > 0 )
count++;
ans = ans * count;
}
return ans;
}
public static void main(String[] args)
{
String s = "aaa" ;
int x = 2 ;
System.out.println(findways(s, x));
}
}
|
Python3
def findways(s, x):
if (x > len (s)):
return 0
n = len (s)
freq = [ 0 ] * 26
for i in range (n):
freq[ ord (s[i]) - ord ( 'a' )] + = 1
se = set ()
for i in range ( 26 ):
if (freq[i] > 0 ):
se.add(freq[i])
ans = 1
for i in range (x / / 2 ):
count = 0
for u in se:
if (u > = 2 ):
count + = 1
if (count = = 0 ):
return 0
else :
ans * = count
p = list (se)
val = p[ - 1 ]
p.pop( - 1 )
se = set (p)
if (val > 2 ):
se.add(val - 2 )
if (x % 2 ! = 0 ):
count = 0
for u in se:
if (u > 0 ):
count + = 1
ans = ans * count
return ans
if __name__ = = '__main__' :
s = "aaa"
x = 2
print (findways(s, x))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int findways( string s, int x)
{
if (x > s.Length)
return 0;
int n = s.Length;
int [] freq = new int [26];
for ( int i = 0; i < n; ++i)
freq[s[i] - 'a' ]++;
HashSet< int > se = new HashSet< int >();
for ( int i = 0; i < 26; ++i)
if (freq[i] > 0)
se.Add(freq[i]);
int ans = 1;
for ( int i = 0; i < x / 2; ++i)
{
int count = 0;
foreach ( int u in se)
{
if (u >= 2)
count++;
}
if (count == 0)
return 0;
else
ans = ans * count;
int [] arr = new int [se.Count];
se.CopyTo(arr);
int p = arr[se.Count - 1];
int val = p;
se.Remove(p);
if (val > 2)
se.Add(val - 2);
}
if (x % 2 != 0)
{
int count = 0;
foreach ( int u in se)
if (u > 0) count++;
ans = ans * count;
}
return ans;
}
static public void Main()
{
string s = "aaa" ;
int x = 2;
Console.WriteLine(findways(s, x));
}
}
|
Javascript
<script>
function findways(s, x)
{
if (x > s.length)
return 0;
var n = s.length;
var freq = Array(26).fill(0);
for ( var i = 0; i < n; ++i)
freq[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
var se = [];
for ( var i = 0; i < 26; ++i)
if (freq[i] > 0)
se.push(freq[i]);
var ans = 1;
for ( var i = 0; i < x / 2; ++i) {
var count = 0;
se.forEach(u => {
if (u >= 2)
count++;
});
if (count == 0)
return 0;
else
ans = ans * count;
se.sort((a,b)=>a-b)
var val = se[se.length-1]
se.pop();
if (val > 2)
se.push(val - 2);
}
if (x % 2 != 0) {
var count = 0;
se.forEach(u => {
if (u > 0)
count++;
});
ans = ans * count;
}
return ans;
}
var s = "aaa" ;
var x = 2;
document.write( findways(s, x));
</script>
|
Time Complexity: O(N + 26 * X)
Last Updated :
03 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...