最多一个字符的频率为奇数的子串计数
给定一个包含N个字符的字符串S ,任务是计算非空子字符串的总数,使得最多一个字符出现奇数次。
示例:
Input: S = “aba”
Output: 4
Explanation: The valid substrings are “a”, “b”, “a”, and “aba”. Therefore, the total number of required substrings are 4.
Input: “aabb”
Output: 9
Explanation: The valid substrings are “a”, “aa”, “aab”, “aabb”, “a”, “abb”, “b”, “bb”, and “b”.
方法:上述问题可以在Bit Masking using HashMaps的帮助下解决。请按照以下步骤解决问题:
- 每个字符频率的奇偶校验可以存储在位掩码掩码中,其中 第i个字符由2 i表示。最初mask = 0的值。
- 创建一个无序的 map seen ,它存储每个位掩码的出现频率。最初, seen[0] = 1的值。
- 创建一个变量cnt ,它存储有效子字符串的计数。最初, cnt = 0的值。
- 迭代 [0, N) 范围内的每个i并将掩码的值与表示字符串的第 i个字符的整数按位异或,并通过seen[mask]增加cnt的值。
- 对于每个有效的i ,遍历[a, z]范围内的所有字符并通过翻转当前掩码中的第 j个设置位来增加其频率,并在翻转第j个位掩码后将cnt的值增加位掩码的频率设置位。
- 存储在cnt中的值是所需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the count of substrings
// such that at most one character occurs
// odd number of times
int uniqueSubstrings(string S)
{
// Stores the frequency of the bitmasks
unordered_map seen;
// Initial Condition
seen[0] = 1;
// Store the current value of the bitmask
int mask = 0;
// Stores the total count of the
// valid substrings
int cnt = 0;
for (int i = 0; i < S.length(); ++i) {
// XOR the mask with current character
mask ^= (1 << (S[i] - 'a'));
// Increment the count by mask count
// of strings with all even frequencies
cnt += seen[mask];
for (int j = 0; j < 26; ++j) {
// Increment count by mask count
// of strings if exist with the
// jth character having odd frequency
cnt += seen[mask ^ (1 << j)];
}
seen[mask]++;
}
// Return Answer
return cnt;
}
// Driver Code
int main()
{
string word = "aabb";
cout << uniqueSubstrings(word);
return 0;
}
Python3
# Python program for the above approach
# Function to find the count of substrings
# such that at most one character occurs
# odd number of times
def uniqueSubstrings(S):
# Stores the frequency of the bitmasks
seen = {}
# Initial Condition
seen[0] = 1
# Store the current value of the bitmask
mask = 0
# Stores the total count of the
# valid substrings
cnt = 0
for i in range(len(S)):
# XOR the mask with current character
mask ^= (1 << (ord(S[i]) - ord('a')))
# Increment the count by mask count
# of strings with all even frequencies
if mask in seen:
cnt += seen[mask]
else:
cnt += 0
for j in range(26):
# Increment count by mask count
# of strings if exist with the
# jth character having odd frequency
if mask ^ (1 << j) in seen:
cnt += seen[mask ^ (1 << j)]
else:
cnt += 0
if mask in seen:
seen[mask] += 1
else:
seen[mask] = 1
# Return Answer
return cnt
# Driver Code
word = "aabb"
print(uniqueSubstrings(word))
# This code is contributed by rj13to.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the count of substrings
// such that at most one character occurs
// odd number of times
static int uniqueSubstrings(string S)
{
// Stores the frequency of the bitmasks
Dictionary seen = new Dictionary();
// Initial Condition
seen[0] = 1;
// Store the current value of the bitmask
int mask = 0;
// Stores the total count of the
// valid substrings
int cnt = 0;
for(int i = 0; i < S.Length; ++i)
{
// XOR the mask with current character
mask ^= (1 << (S[i] - 'a'));
// Increment the count by mask count
// of strings with all even frequencies
if (seen.ContainsKey(mask))
cnt += seen[mask];
for(int j = 0; j < 26; ++j)
{
// Increment count by mask count
// of strings if exist with the
// jth character having odd frequency
if (seen.ContainsKey(mask ^ (1 << j)))
cnt += seen[mask ^ (1 << j)];
}
if (seen.ContainsKey(mask))
seen[mask]++;
else
seen[mask] = 1;
}
// Return Answer
return cnt;
}
// Driver Code
public static void Main()
{
string word = "aabb";
Console.WriteLine(uniqueSubstrings(word));
}
}
// This code is contributed by ukasp
输出:
9
时间复杂度:O(N)
辅助空间:O(N)