给定字符串str仅包含小写字母和整数K ,任务是计算大小为K的子字符串的数量,以使子字符串的任何排列都是回文。
例子:
Input: str = “abbaca”, K = 3
Output: 3
Explanation:
The substrings of size 3 whose any permutation is palindrome are {“abb”, “bba”, “aca”}.
Input: str = “aaaa”, K = 1
Output: 4
Explanation:
The substrings of size 1 whose any permutation is palindrome are {‘a’, ‘a’, ‘a’, ‘a’}.
天真的方法:天真的解决方案是运行两个循环以生成所有大小为K的子串。对于形成的每个子串,找到子串每个字符的频率。如果最多一个字符的频率为奇数,则其排列之一将是回文。在所有操作之后,递增当前子字符串的计数并打印最终计数。
时间复杂度: O(N * K)
高效的方法:使用窗口滑动技术并使用大小为26的频率阵列可以有效地解决此问题。步骤如下:
- 将给定字符串的前K个元素的频率存储在频率数组中(例如freq [] )。
- 使用频率数组,检查具有奇数频率的元素的数量,如果它小于2 ,则递增
回文排列计数。 - 现在,向前线性滑动窗口,直到到达终点为止。
- 在每次迭代中,通过1减小窗口的第一元素的计数和增加1窗口的下一个元素的计数,并再次检查具有奇数频率在频率数组元素的计数,如果是小于2 ,然后增加回文排列的计数。
- 重复上述步骤,直到到达字符串的末尾并打印回文排列的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// To store the frequency array
vector freq(26);
// Function to check palindromic of
// of any substring using frequency array
bool checkPalindrome()
{
// Initialise the odd count
int oddCnt = 0;
// Traversing frequency array to
// compute the count of characters
// having odd frequency
for (auto x : freq) {
if (x % 2 == 1)
oddCnt++;
}
// Returns true if odd count is atmost 1
return oddCnt <= 1;
}
// Function to count the total number
// substring whose any permutations
// are palindromic
int countPalindromePermutation(
string s, int k)
{
// Computing the frequncy of
// first K character of the string
for (int i = 0; i < k; i++) {
freq[s[i] - 97]++;
}
// To store the count of
// palindromic permutations
int ans = 0;
// Checking for the current window
// if it has any palindromic
// permutation
if (checkPalindrome()) {
ans++;
}
// Start and end point of window
int i = 0, j = k;
while (j < s.size()) {
// Sliding window by 1
// Decrementing count of first
// element of the window
freq[s[i++] - 97]--;
// Incrementing count of next
// element of the window
freq[s[j++] - 97]++;
// Checking current window
// character frequency count
if (checkPalindrome()) {
ans++;
}
}
// Return the final count
return ans;
}
// Driver Code
int main()
{
// Given string str
string str = "abbaca";
// Window of size K
int K = 3;
// Function Call
cout << countPalindromePermutation(str, K)
<< endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// To store the frequency array
static int []freq = new int[26];
// Function to check palindromic of
// of any subString using frequency array
static boolean checkPalindrome()
{
// Initialise the odd count
int oddCnt = 0;
// Traversing frequency array to
// compute the count of characters
// having odd frequency
for(int x : freq)
{
if (x % 2 == 1)
oddCnt++;
}
// Returns true if odd count
// is atmost 1
return oddCnt <= 1;
}
// Function to count the total number
// subString whose any permutations
// are palindromic
static int countPalindromePermutation(char []s,
int k)
{
// Computing the frequncy of
// first K character of the String
for(int i = 0; i < k; i++)
{
freq[s[i] - 97]++;
}
// To store the count of
// palindromic permutations
int ans = 0;
// Checking for the current window
// if it has any palindromic
// permutation
if (checkPalindrome())
{
ans++;
}
// Start and end point of window
int i = 0, j = k;
while (j < s.length)
{
// Sliding window by 1
// Decrementing count of first
// element of the window
freq[s[i++] - 97]--;
// Incrementing count of next
// element of the window
freq[s[j++] - 97]++;
// Checking current window
// character frequency count
if (checkPalindrome())
{
ans++;
}
}
// Return the final count
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "abbaca";
// Window of size K
int K = 3;
// Function Call
System.out.print(countPalindromePermutation(
str.toCharArray(), K) + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# To store the frequency array
freq = [0] * 26
# Function to check palindromic of
# of any substring using frequency array
def checkPalindrome():
# Initialise the odd count
oddCnt = 0
# Traversing frequency array to
# compute the count of characters
# having odd frequency
for x in freq:
if (x % 2 == 1):
oddCnt += 1
# Returns true if odd count is atmost 1
return oddCnt <= 1
# Function to count the total number
# substring whose any permutations
# are palindromic
def countPalindromePermutation(s, k):
# Computing the frequncy of
# first K character of the string
for i in range(k):
freq[ord(s[i]) - 97] += 1
# To store the count of
# palindromic permutations
ans = 0
# Checking for the current window
# if it has any palindromic
# permutation
if (checkPalindrome()):
ans += 1
# Start and end poof window
i = 0
j = k
while (j < len(s)):
# Sliding window by 1
# Decrementing count of first
# element of the window
freq[ord(s[i]) - 97] -= 1
i += 1
# Incrementing count of next
# element of the window
freq[ord(s[j]) - 97] += 1
j += 1
# Checking current window
# character frequency count
if (checkPalindrome()):
ans += 1
# Return the final count
return ans
# Driver Code
# Given string str
str = "abbaca"
# Window of size K
K = 3
# Function call
print(countPalindromePermutation(str, K))
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG{
// To store the frequency array
static int []freq = new int[26];
// Function to check palindromic of
// of any subString using frequency array
static bool checkPalindrome()
{
// Initialise the odd count
int oddCnt = 0;
// Traversing frequency array to
// compute the count of characters
// having odd frequency
foreach(int x in freq)
{
if (x % 2 == 1)
oddCnt++;
}
// Returns true if odd count
// is atmost 1
return oddCnt <= 1;
}
// Function to count the total number
// subString whose any permutations
// are palindromic
static int countPalindromePermutation(char []s,
int k)
{
int i = 0;
// Computing the frequncy of
// first K character of the String
for(i = 0; i < k; i++)
{
freq[s[i] - 97]++;
}
// To store the count of
// palindromic permutations
int ans = 0;
// Checking for the current window
// if it has any palindromic
// permutation
if (checkPalindrome())
{
ans++;
}
// Start and end point of window
int j = k;
i = 0;
while (j < s.Length)
{
// Sliding window by 1
// Decrementing count of first
// element of the window
freq[s[i++] - 97]--;
// Incrementing count of next
// element of the window
freq[s[j++] - 97]++;
// Checking current window
// character frequency count
if (checkPalindrome())
{
ans++;
}
}
// Return the final count
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "abbaca";
// Window of size K
int K = 3;
// Function Call
Console.Write(countPalindromePermutation(
str.ToCharArray(), K) + "\n");
}
}
// This code is contributed by Amit Katiyar
输出:
3
时间复杂度: O(N)
辅助空间: O(26)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。