给定字符串str仅由小写字母和整数K 组成,任务是计算大小为K的子串的数量,使得子串的任何排列都是回文。
例子:
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’}.
朴素的方法:朴素的解决方案是运行两个循环来生成大小为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
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(26)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live