Count of K-size substrings having palindromic permutations
Last Updated :
08 Nov, 2023
Given string str consists of only lowercase alphabets and an integer K, the task is to count the number of substrings of size K such that any permutation of the substring is a palindrome.
Examples:
Input: str = “abbaca”, K = 3
Output: 3
Explanation:
The substrings of size 3 whose permutation is palindrome are {“abb”, “bba”, “aca”}.
Input: str = “aaaa”, K = 1
Output: 4
Explanation:
The substrings of size 1 whose permutation is palindrome are {‘a’, ‘a’, ‘a’, ‘a’}.
Naive Approach: A naive solution is to run a two-loop to generate all substrings of size K. For each substring formed, find the frequency of each character of the substring. If at most one character has an odd frequency, then one of its permutations will be a palindrome. Increment the count for the current substring and print the final count after all the operations.
Time Complexity: O(N*K)
Count of K-size substrings having palindromic permutations using Sliding Window Technique:
The idea is to use the Window Sliding Technique and using a frequency array of size 26.
Step-by-step approach:
- Store the frequency of the first K elements of the given string in a frequency array(say freq[]).
- Using a frequency array, check the count of elements having an odd frequency. If it is less than 2, then the increment of the count of palindromic permutation.
- Now, linearly slide the window ahead till it reaches the end.
- At each iteration, decrease the count of the first element of the window by 1 and increase the count of the next element of the window by 1 and again check the count of elements in a frequency array having an odd frequency. If it is less than 2, then increase the count of the palindromic permutation.
- Repeat the above step till we reach the end of the string and print the count of palindromic permutation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > freq(26);
bool checkPalindrome()
{
int oddCnt = 0;
for ( auto x : freq) {
if (x % 2 == 1)
oddCnt++;
}
return oddCnt <= 1;
}
int countPalindromePermutation(
string s, int k)
{
for ( int i = 0; i < k; i++) {
freq[s[i] - 97]++;
}
int ans = 0;
if (checkPalindrome()) {
ans++;
}
int i = 0, j = k;
while (j < s.size()) {
freq[s[i++] - 97]--;
freq[s[j++] - 97]++;
if (checkPalindrome()) {
ans++;
}
}
return ans;
}
int main()
{
string str = "abbaca" ;
int K = 3;
cout << countPalindromePermutation(str, K)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int []freq = new int [ 26 ];
static boolean checkPalindrome()
{
int oddCnt = 0 ;
for ( int x : freq)
{
if (x % 2 == 1 )
oddCnt++;
}
return oddCnt <= 1 ;
}
static int countPalindromePermutation( char []s,
int k)
{
for ( int i = 0 ; i < k; i++)
{
freq[s[i] - 97 ]++;
}
int ans = 0 ;
if (checkPalindrome())
{
ans++;
}
int i = 0 , j = k;
while (j < s.length)
{
freq[s[i++] - 97 ]--;
freq[s[j++] - 97 ]++;
if (checkPalindrome())
{
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
String str = "abbaca" ;
int K = 3 ;
System.out.print(countPalindromePermutation(
str.toCharArray(), K) + "\n" );
}
}
|
Python3
freq = [ 0 ] * 26
def checkPalindrome():
oddCnt = 0
for x in freq:
if (x % 2 = = 1 ):
oddCnt + = 1
return oddCnt < = 1
def countPalindromePermutation(s, k):
for i in range (k):
freq[ ord (s[i]) - 97 ] + = 1
ans = 0
if (checkPalindrome()):
ans + = 1
i = 0
j = k
while (j < len (s)):
freq[ ord (s[i]) - 97 ] - = 1
i + = 1
freq[ ord (s[j]) - 97 ] + = 1
j + = 1
if (checkPalindrome()):
ans + = 1
return ans
str = "abbaca"
K = 3
print (countPalindromePermutation( str , K))
|
C#
using System;
class GFG{
static int []freq = new int [26];
static bool checkPalindrome()
{
int oddCnt = 0;
foreach ( int x in freq)
{
if (x % 2 == 1)
oddCnt++;
}
return oddCnt <= 1;
}
static int countPalindromePermutation( char []s,
int k)
{
int i = 0;
for (i = 0; i < k; i++)
{
freq[s[i] - 97]++;
}
int ans = 0;
if (checkPalindrome())
{
ans++;
}
int j = k;
i = 0;
while (j < s.Length)
{
freq[s[i++] - 97]--;
freq[s[j++] - 97]++;
if (checkPalindrome())
{
ans++;
}
}
return ans;
}
public static void Main(String[] args)
{
String str = "abbaca" ;
int K = 3;
Console.Write(countPalindromePermutation(
str.ToCharArray(), K) + "\n" );
}
}
|
Javascript
<script>
var freq = Array(26).fill(0);
function checkPalindrome()
{
var oddCnt = 0;
freq.forEach(x => {
if (x % 2 == 1)
oddCnt++;
});
return oddCnt <= 1;
}
function countPalindromePermutation( s, k)
{
for ( var i = 0; i < k; i++) {
freq[s[i].charCodeAt(0) - 97]++;
}
var ans = 0;
if (checkPalindrome()) {
ans++;
}
var i = 0, j = k;
while (j < s.length) {
freq[s[i++].charCodeAt(0) - 97]--;
freq[s[j++].charCodeAt(0) - 97]++;
if (checkPalindrome()) {
ans++;
}
}
return ans;
}
var str = "abbaca" ;
var K = 3;
document.write( countPalindromePermutation(str, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...