Count of substrings with the frequency of at most one character as Odd in C++17 Mar 2025 | 4 min read 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-
Substrings that include a maximum of one character and appear at odd intervals can now be
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 SolutionThis 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: ![]() 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-maskingBit-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: ![]() 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. |
Boost C++ What is Boost in C++? An open-source library set for C++ programming is called Boost. It gives the C++ language extra features, correcting shortcomings and allowing for more effective programming. A wide range of libraries from the Boost library collection can be used to streamline C++...
16 min read
Activity Selection is a classical problem in computer science, which can be solved using greedy algorithms. In this problem, we are given a set of activities that are to be performed during a given period, and each activity has a start time and an end time....
3 min read
Let's assume we have two non-negative numbers, x and y, as well as two values, l and r. We have to determine whether or not all bits in the range l to r of both given numbers are complements of one another. We will learn how to...
2 min read
What is Fibonacci Series The Fibonacci numbers are the numbers in the integer sequence shown below. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. The recurrence relation defines the sequence Fn of Fibonacci numbers in mathematical terms. Fn = Fn-1 + Fn-2 with seed values F0...
2 min read
In this article, you will learn about the symbol table in C++. Compiler Design symbol table In order to store information on the existence of different entities, such as variable and function names, objects and classes, etc., the compiler builds and maintains a data structure. Symbol tables are...
5 min read
In this article, you will learn about the with its example. iswblank() function: The C Standard Library includes the iswblank() function, which can be found in the <wctype.h> header file. Iswblank() is intended to support wide characters (wchar_t) in C, unlike isblank() in the standard <ctype.h> library....
4 min read
This C program uses matrix multiplication to encode a message. This type of coding uses a big matrix to encrypt a message, and it is very hard to break. The message's recipient decodes it by using the matrix's inverse. The encoding matrix is the first matrix,...
2 min read
In C++ language, fallthrough refers to the behaviour in a switch statement where the control flows from one case to another. It occurs when there is no break statement at the end of a case, allowing the control to continue to the case. In programming control,...
5 min read
C++ gives an effective and flexible set of equipment for builders, and one often disregarded gem is the forward_list magnificence. Among its many capabilities, the forward_list::splice_after() feature stands proud as an effective device for manipulating linked lists. In this blog post, we're going to explore the...
4 min read
Whenever a function is defined in a program written in the C++ language. If we want to call the function, it can be done in two ways that are: Call by value Call by reference Before discussing the call-by-reference method, we will know about both ways to call the...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India