Count of substrings with the frequency of at most one character as Odd in C++

In this article, we will discuss how to count of substrings with the frequency of at most one character as Odd in C++ with different approaches.

Character subsets or sequences that are contiguous within a string are called substrings.

It is now necessary to ascertain the number of substrings in this problem that have at most one odd character as their frequency. Let's look at the most effective way to handle this situation.

Let's attempt to make sense of this issue with a few instances.

Input:

Output:

21

Explanation: As the frequency of the characters in the given string is given below-

  • k → 3
  • s → 3
  • j → 2

Substrings that include a maximum of one character and appear at odd intervals can now be

  • Take each character: 'k', 's', 'j', 's', 's', 'j', 'k', 'k' = 8
  • Take 2 letters at a time: 'ss', 'kk' = 2
  • Take 3 letters at a time: 'sjs', 'jss', 'ssj', 'jkk' = 4
  • Take 4 letters at a time: 'jssj' = 1
  • Take 5 letters at a time: 'sjssj', 'jssjk', 'ssjkk' = 3
  • Take 6 letters at a time: 'jssjkk' = 1
  • Take 7 letters at a time: 'ksjssjk', 'sjssjkk' = 2
  • Take 8 letters at a time: No string

Now, adding the number of substrings we get (8 + 2 + 4 + 1 + 3 + 1 + 2) = 21

Input:

Output:

2

Explanation: here, you will get 2 substrings: 'a', 'b'

Problem Statement:

Let us try to comprehend the issue and identify a workable answer. Finding the substrings in the string where, on average, one letter appears an odd number of times is necessary; there should only be one oddly frequent letter overall.

Solution-1: Brute Force Solution

This method is simple to comprehend. In this approach, we will only execute loops and continuously verify whether there is a single letter with an odd frequency. If so, the substring will be included in the finished product.

Example:

Output:

Count of substrings with the frequency of at most one character as Odd in C++

Complexity Analysis:

Time complexity:

O(n^3); where n is the string's length and (n^3) is the helper function's time complexity, whereas (O(n)) is the checkValid function's execution time.

Space complexity:

O(1); The code above does not contain any variables stored in a data structure.

Solution 2: Optimized Solution using Bit-masking

Bit-masking

Bitmasking is a process that is used to apply a mask over a value to preserve, alter, or modify a portion of given data. A mask helps to select which bits to be kept or which to be removed from a binary integer. By applying different bitwise operations, it can be used to mask a value to represent the subsets of a set.

Approach:

We use a bitmask to identify which characters are used the odd number of times. We use a hashmap to store previously viewed bitmasks. After every iteration, we will raise the hashmap[bitmask] by one to show that we are familiar with this bitmask. When output += m[mask], the substrings with an even number of letters used will be tallied. When exactly one letter appears oddly, output+= m[mask^ (1\<j)] will count the substrings.

Example:

Output:

Count of substrings with the frequency of at most one character as Odd in C++

Complexity Analysis:

Time complexity:

O(n*26); n is the string's length. We have a check for 26 characters for each character in the string.

Space complexity:

O(1): The only data structure we utilized was a map, which takes up O(26) space, which is roughly equivalent to O(1) space complexity.

Conclusion:

First, we will use the simplistic method of employing loops to obtain the desired result. This method is simple to comprehend, but it has the disadvantage of requiring much processing time to complete. But, we can quickly determine the code's temporal complexity by employing a different method known as bitmasking with hashmaps. The bitmasking approach was applied unusually in this particular topic, which reduced the time complexity from O(n^3) to O(n). In this article, we studied the principles and application of bitmasking.