Given a string S, containing lowercase English alphabets, and an integer K, the task is to find any index of the string which consists of more than K active characters. If found, print Yes. Otherwise, print No.
Count of active characters for any index is the number of characters having previous occurrences before or at the current index and last occurrence at or after the current index.
Input: S = “aabbcd”, K = 1
Index 1: Active character: a
Index 2: Active character: a
Index 3: Active character: b
Index 4: Active character: b
Index 5: Active character: c
Index 6: Active character: d
There are no more than one active character at any index in the string.
Input: S = “aabbcdca”, K = 2\
Index 1: Active character : a
Index 2: Active character : a
Index 3: Active characters : a, b
Index 4: Active characters : a, b
Index 5: Active characters : a, c
Index 6: Active characters : a, c, d
Therefore, there exists an index with more than 2 active characters.
Follow the steps below to solve the problem:
- The idea is to store the last occurrence of each character present in the string in a Map.
- Traverse the string and keep storing active letters int a Set.
- If at any index, the size of the Set exceeds K, print “Yes”.
- Otherwise, check if the current index is the last occurrence of the current character. If so, remove the character from the Set.
- Finally, if no index is found with more than K active characters, print “No”.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether second string can be formed from characters of first string
- Check if both halves of the string have same set of characters
- Quick way to check if all the characters of a string are same
- Program to check if first and the last characters of string are equal
- Check if characters of one string can be swapped to form other
- Check whether the frequencies of all the characters in a string are prime or not
- Check if a given string is made up of two alternating characters
- Check if both halves of the string have same set of characters in Python
- Check if a string is made up of K alternating characters
- Check if the characters of a given string are in alphabetical order
- Check if characters of a given string can be rearranged to form a palindrome
- Python program to check if a string contains all unique characters
- Check if string follows order of characters defined by a pattern or not | Set 1
- Check whether count of distinct characters in a string is Prime or not
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if a string can be split into substrings starting with N followed by N characters
- Check if a string has all characters with same frequency with one variation allowed
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Check if a given string can be formed using characters of adjacent cells of a Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : offbeat