Count of substrings with the frequency of at most one character as Odd
Last Updated :
03 Apr, 2023
Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times.
Example:
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".
Approach: The above problem can be solved with the help of Bit Masking using HashMaps. Follow the below-mentioned steps to solve the problem:
- The parity of the frequency of each character can be stored in a bitmask mask, where the ith character is represented by 2i. Initially the value of mask = 0.
- Create an unordered map seen, which stores the frequency of occurrence of each bitmask. Initially, the value of seen[0] = 1.
- Create a variable cnt, which stores the count of the valid substrings. Initially, the value of cnt = 0.
- Iterate for each i in the range [0, N) and Bitwise XOR the value of the mask with the integer representing the ith character of the string and increment the value of cnt by seen[mask].
- For each valid i, Iterate through all characters in the range [a, z] and increase its frequency by flipping the jth set-bit in the current mask and increment the value of the cnt by the frequency of bitmask after flipping the jth set-bit.
- The value stored in cnt is the required answer.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
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<int, int> 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;
}
Java
import java.util.*;
public class UniqueSubstrings {
// Function to find the count of substrings
// such that at most one character occurs
// odd number of times
public static int uniqueSubstrings(String S)
{
// Stores the frequency of the bitmasks
HashMap<Integer, Integer> seen = new HashMap<>();
// Initial Condition
seen.put(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.charAt(i) - 'a'));
// Increment the count by mask count
// of strings with all even frequencies
if (seen.containsKey(mask)) {
cnt += seen.get(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.get(mask ^ (1 << j));
}
}
seen.put(mask, seen.getOrDefault(mask, 0) + 1);
}
// Return Answer
return cnt;
}
// Driver Code
public static void main(String[] args)
{
String word = "aabb";
System.out.println(uniqueSubstrings(word));
}
}
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<int,
int> seen = new Dictionary<int,
int>();
// 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
JavaScript
// Javascript program for the above approach
// Function to find the count of substrings
// such that at most one character occurs
// odd number of times
function uniqueSubstrings(S)
{
// Stores the frequency of the bitmasks
let seen = new Map();
// Initial Condition
seen.set(0, 1);
// Store the current value of the bitmask
let mask = 0;
// Stores the total count of the
// valid substrings
let cnt = 0;
for(let i = 0; i < S.length; ++i)
{
// XOR the mask with current character
mask ^= (1 << (S[i].charCodeAt(0) - 'a'.charCodeAt(0)));
// Increment the count by mask count
// of strings with all even frequencies
if (seen.has(mask))
cnt += seen.get(mask);
for(let j = 0; j < 26; ++j)
{
// Increment count by mask count
// of strings if exist with the
// jth character having odd frequency
if (seen.has(mask ^ (1 << j)))
cnt += seen.get(mask ^ (1 << j));
}
if (seen.has(mask))
seen.set(mask, seen.get(mask) + 1);
else
seen.set(mask, 1);
}
// Return Answer
return cnt;
}
// Driver Code
let word = "aabb";
document.write(uniqueSubstrings(word));
// This code is contributed by Saurabh Jaiswal
Time Complexity: O(N*K)
Auxiliary Space: O(N)
Similar Reads
Count substrings with each character occurring at most k times Given a string S. Count number of substrings in which each character occurs at most k times. Assume that the string consists of only lowercase English alphabets. Examples: Input : S = ab k = 1 Output : 3 All the substrings a, b, ab have individual character count less than 1. Input : S = aaabb k = 2
15+ min read
Count Substrings with even frequency of each character and one exception Given a string S ('a' ? S[i] ? 't') of length N (1 ? N ? 105), which consists of lowercase English alphabets, the task is to count all substrings such that the frequency of all characters should be even or all characters should occur an even number of times except any one character which might occur
6 min read
Count of Substrings that can be formed without using the given list of Characters Given a string str and a list of characters L, the task is to count the total numbers of substrings of the string str without using characters given in the list L. Examples: Input: str = "abcd", L[] = {'a', 'b', 't', 'q'} Output: 3 Explanation: On ignoring the characters 'a' and 'b' from the given s
7 min read
Count odd length Substrings with median same as Kth character of String Given a string S of size N, the task is to find the number of substrings of odd lengths that have a median equal to the Kth character of the string. Examples: Input: S = "ecadgg", K = 4Output: 4Explanation: Character at 4th position in string is 'd'. Then there are 4 odd length substrings with 'd' a
11 min read
Count of substrings formed using a given set of characters only Given a string str and an array arr[] of K characters, the task is to find the number of substrings of str that contain characters only from the given character array arr[]. Note: The string str and the arr[] contain only lowercase alphabets. Examples: Input: S = "abcb", K = 2, charArray[] = {'a', '
8 min read