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++
#include <bits/stdc++.h>
using namespace std;
int uniqueSubstrings(string S)
{
unordered_map< int , int > seen;
seen[0] = 1;
int mask = 0;
int cnt = 0;
for ( int i = 0; i < S.length(); ++i) {
mask ^= (1 << (S[i] - 'a' ));
cnt += seen[mask];
for ( int j = 0; j < 26; ++j) {
cnt += seen[mask ^ (1 << j)];
}
seen[mask]++;
}
return cnt;
}
int main()
{
string word = "aabb" ;
cout << uniqueSubstrings(word);
return 0;
}
|
Java
import java.util.*;
public class UniqueSubstrings {
public static int uniqueSubstrings(String S)
{
HashMap<Integer, Integer> seen = new HashMap<>();
seen.put( 0 , 1 );
int mask = 0 ;
int cnt = 0 ;
for ( int i = 0 ; i < S.length(); ++i) {
mask ^= ( 1 << (S.charAt(i) - 'a' ));
if (seen.containsKey(mask)) {
cnt += seen.get(mask);
}
for ( int j = 0 ; j < 26 ; ++j) {
if (seen.containsKey(mask ^ ( 1 << j))) {
cnt += seen.get(mask ^ ( 1 << j));
}
}
seen.put(mask, seen.getOrDefault(mask, 0 ) + 1 );
}
return cnt;
}
public static void main(String[] args)
{
String word = "aabb" ;
System.out.println(uniqueSubstrings(word));
}
}
|
Python3
def uniqueSubstrings(S):
seen = {}
seen[ 0 ] = 1
mask = 0
cnt = 0
for i in range ( len (S)):
mask ^ = ( 1 << ( ord (S[i]) - ord ( 'a' )))
if mask in seen:
cnt + = seen[mask]
else :
cnt + = 0
for j in range ( 26 ):
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 cnt
word = "aabb"
print (uniqueSubstrings(word))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int uniqueSubstrings( string S)
{
Dictionary< int ,
int > seen = new Dictionary< int ,
int >();
seen[0] = 1;
int mask = 0;
int cnt = 0;
for ( int i = 0; i < S.Length; ++i)
{
mask ^= (1 << (S[i] - 'a' ));
if (seen.ContainsKey(mask))
cnt += seen[mask];
for ( int j = 0; j < 26; ++j)
{
if (seen.ContainsKey(mask ^ (1 << j)))
cnt += seen[mask ^ (1 << j)];
}
if (seen.ContainsKey(mask))
seen[mask]++;
else
seen[mask] = 1;
}
return cnt;
}
public static void Main()
{
string word = "aabb" ;
Console.WriteLine(uniqueSubstrings(word));
}
}
|
Javascript
function uniqueSubstrings(S)
{
let seen = new Map();
seen.set(0, 1);
let mask = 0;
let cnt = 0;
for (let i = 0; i < S.length; ++i)
{
mask ^= (1 << (S[i].charCodeAt(0) - 'a' .charCodeAt(0)));
if (seen.has(mask))
cnt += seen.get(mask);
for (let j = 0; j < 26; ++j)
{
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 cnt;
}
let word = "aabb" ;
document.write(uniqueSubstrings(word));
|
Time Complexity: O(N*K)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...