给定一个由N个小写字母组成的字符串S ,任务是计算每个字符频率为偶数的子字符串的数量。
例子:
Input: S = “abbaa”
Output: 4
Explanation:
The substrings having frequency of each character is even are {“abba”, “aa”, “bb”, “bbaa”}.
Therefore, the count is 4.
Input: S = “geeksforgeeks”
Output: 2
朴素方法:解决给定问题的最简单方法是生成给定字符串的所有可能子字符串 并计算每个字符具有偶数频率的那些子串。检查所有子字符串后,打印获得的总数。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG
{
// Function to count substrings having
// even frequency of each character
static int subString(String s, int n)
{
// Stores the total
// count of substrings
int count = 0;
// Traverse the range [0, N]:
for (int i = 0; i < n; i++) {
// Traverse the range [i + 1, N]
for (int len = i + 1; len <= n; len++) {
// Stores the substring over
// the range of indices [i, len]
String test_str = s.substring(i, len);
// Stores the frequency of characters
HashMap res
= new HashMap<>();
// Count frequency of each character
for (char keys : test_str.toCharArray()) {
res.put(keys,
res.getOrDefault(keys, 0) + 1);
}
int flag = 0;
// Traverse the dictionary
for (char keys : res.keySet()) {
// If any of the keys
// have odd count
if (res.get(keys) % 2 != 0) {
flag = 1;
break;
}
}
// Otherwise
if (flag == 0)
count += 1;
}
}
// Return count
return count;
}
// Driver Code
public static void main(String[] args)
{
String S = "abbaa";
int N = S.length();
System.out.println(subString(S, N));
}
}
// This code is contributed by Kingash.
Python3
# Python program for the above approach
# Function to count substrings having
# even frequency of each character
def subString(s, n):
# Stores the total
# count of substrings
count = 0
# Traverse the range [0, N]:
for i in range(n):
# Traverse the range [i + 1, N]
for len in range(i + 1, n + 1):
# Stores the substring over
# the range of indices [i, len]
test_str = (s[i: len])
# Stores the frequency of characters
res = {}
# Count frequency of each character
for keys in test_str:
res[keys] = res.get(keys, 0) + 1
flag = 0
# Traverse the dictionary
for keys in res:
# If any of the keys
# have odd count
if res[keys] % 2 != 0:
flag = 1
break
# Otherwise
if flag == 0:
count += 1
# Return count
return count
# Driver Code
S = "abbaa"
N = len(S)
print(subString(S, N))
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count substrings having
// even frequency of each character
int subString(string s, int n)
{
// Stores the count of a character
map hash;
hash[0] = 1;
// Stores bitmask
int pre = 0;
// Stores the count of substrings
// with even count of each character
int count = 0;
// Traverse the string S
for (int i = 0; i < n; i++) {
// Flip the ord(i)-97 bits in pre
pre ^= (1 << int(s[i]) - 97);
// Increment the count by hash[pre]
count += hash[pre];
// Increment count of pre in hash
hash[pre] = hash[pre] + 1;
}
// Return the total count obtained
return count;
}
// Driver Code
int main()
{
string S = "abbaa";
int N = S.length();
cout << (subString(S, N));
}
// THIS CODE IS CONTRIBUTED BY UKASP.
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to count substrings having
// even frequency of each character
static int subString(String s, int n)
{
// Stores the count of a character
Map hash = new HashMap<>();
hash.put(0, 1);
// Stores bitmask
int pre = 0;
// Stores the count of substrings
// with even count of each character
int count = 0;
// Traverse the string S
for(int i = 0; i < n; i++)
{
// Flip the ord(i)-97 bits in pre
pre ^= (1 << (int)(s.charAt(i) - 97));
// Increment the count by hash[pre]
count += hash.getOrDefault(pre, 0);
// Increment count of pre in hash
hash.put(pre, hash.getOrDefault(pre, 0) + 1);
}
// Return the total count obtained
return count;
}
// Driver code
public static void main(String[] args)
{
String S = "abbaa";
int N = S.length();
System.out.print(subString(S, N));
}
}
// This code is contributed by offbeat
Python3
# Python program for the above approach
# Function to count substrings having
# even frequency of each character
def subString(s, n):
# Stores the count of a character
hash = {0: 1}
# Stores bitmask
pre = 0
# Stores the count of substrings
# with even count of each character
count = 0
# Traverse the string S
for i in s:
# Flip the ord(i)-97 bits in pre
pre ^= (1 << ord(i) - 97)
# Increment the count by hash[pre]
count += hash.get(pre, 0)
# Increment count of pre in hash
hash[pre] = hash.get(pre, 0) + 1
# Return the total count obtained
return count
# Driver Code
S = "abbaa"
N = len(S)
print(subString(S, N))
C#
// C# program for the above approach
using System.IO;
using System;
using System.Collections.Generic;
class GFG{
// Function to count substrings having
// even frequency of each character
static int subString(string s, int n)
{
// Stores the count of a character
Dictionary hash = new Dictionary();
hash[0] = 1;
// Stores bitmask
int pre = 0;
// Stores the count of substrings
// with even count of each character
int count = 0;
// Traverse the string S
for(int i = 0; i < n; i++)
{
// Flip the ord(i)-97 bits in pre
pre ^= (1 << (int)(s[i]) - 97);
// Increment the count by hash[pre]
if (hash.ContainsKey(pre))
count += hash[pre];
else
count += 0;
// Increment count of pre in hash
if (hash.ContainsKey(pre))
hash[pre] = hash[pre] + 1;
else
hash.Add(pre, 1);
}
// Return the total count obtained
return count;
}
// Driver code
static void Main()
{
String S = "abbaa";
int N = S.Length;
Console.WriteLine(subString(S, N));
}
}
// This code is contributed by sk944795
输出:
4
时间复杂度: O(N 2 * 26)
辅助空间: O(N)
高效的方法:上述方法可以通过使用Bitmasking的概念进行优化 和 字典。请按照以下步骤解决问题:
- 初始化一个字典,比如hash来存储一个字符的计数。
- 初始化两个变量,比如count为0和pre为0以存储每个字符的偶数计数的子字符串的总数,并存储包含在子字符串中的字符的掩码。
- 遍历给定的字符串并执行以下步骤:
- 翻转变量pre中的第(S[i] – ‘a’)位。
- 通过递增散列[预]的计数和预先在哈希计数。
- 完成以上步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count substrings having
// even frequency of each character
int subString(string s, int n)
{
// Stores the count of a character
map hash;
hash[0] = 1;
// Stores bitmask
int pre = 0;
// Stores the count of substrings
// with even count of each character
int count = 0;
// Traverse the string S
for (int i = 0; i < n; i++) {
// Flip the ord(i)-97 bits in pre
pre ^= (1 << int(s[i]) - 97);
// Increment the count by hash[pre]
count += hash[pre];
// Increment count of pre in hash
hash[pre] = hash[pre] + 1;
}
// Return the total count obtained
return count;
}
// Driver Code
int main()
{
string S = "abbaa";
int N = S.length();
cout << (subString(S, N));
}
// THIS CODE IS CONTRIBUTED BY UKASP.
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to count substrings having
// even frequency of each character
static int subString(String s, int n)
{
// Stores the count of a character
Map hash = new HashMap<>();
hash.put(0, 1);
// Stores bitmask
int pre = 0;
// Stores the count of substrings
// with even count of each character
int count = 0;
// Traverse the string S
for(int i = 0; i < n; i++)
{
// Flip the ord(i)-97 bits in pre
pre ^= (1 << (int)(s.charAt(i) - 97));
// Increment the count by hash[pre]
count += hash.getOrDefault(pre, 0);
// Increment count of pre in hash
hash.put(pre, hash.getOrDefault(pre, 0) + 1);
}
// Return the total count obtained
return count;
}
// Driver code
public static void main(String[] args)
{
String S = "abbaa";
int N = S.length();
System.out.print(subString(S, N));
}
}
// This code is contributed by offbeat
蟒蛇3
# Python program for the above approach
# Function to count substrings having
# even frequency of each character
def subString(s, n):
# Stores the count of a character
hash = {0: 1}
# Stores bitmask
pre = 0
# Stores the count of substrings
# with even count of each character
count = 0
# Traverse the string S
for i in s:
# Flip the ord(i)-97 bits in pre
pre ^= (1 << ord(i) - 97)
# Increment the count by hash[pre]
count += hash.get(pre, 0)
# Increment count of pre in hash
hash[pre] = hash.get(pre, 0) + 1
# Return the total count obtained
return count
# Driver Code
S = "abbaa"
N = len(S)
print(subString(S, N))
C#
// C# program for the above approach
using System.IO;
using System;
using System.Collections.Generic;
class GFG{
// Function to count substrings having
// even frequency of each character
static int subString(string s, int n)
{
// Stores the count of a character
Dictionary hash = new Dictionary();
hash[0] = 1;
// Stores bitmask
int pre = 0;
// Stores the count of substrings
// with even count of each character
int count = 0;
// Traverse the string S
for(int i = 0; i < n; i++)
{
// Flip the ord(i)-97 bits in pre
pre ^= (1 << (int)(s[i]) - 97);
// Increment the count by hash[pre]
if (hash.ContainsKey(pre))
count += hash[pre];
else
count += 0;
// Increment count of pre in hash
if (hash.ContainsKey(pre))
hash[pre] = hash[pre] + 1;
else
hash.Add(pre, 1);
}
// Return the total count obtained
return count;
}
// Driver code
static void Main()
{
String S = "abbaa";
int N = S.Length;
Console.WriteLine(subString(S, N));
}
}
// This code is contributed by sk944795
输出:
4
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live