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 if the frequency of any character is more than half the length of the string
- Remove characters that appear more than k times
- Python program to check if a string contains all unique characters
- Check if a string contains uppercase, lowercase, special characters and numeric values
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Check three or more consecutive identical characters or numbers
- String matching where one string contains wildcard characters
- Ways of transforming one string to other by removing 0 or more characters
- Replace every character of string by character whose ASCII value is K times more than it
- Strings formed from given characters without any consecutive repeating characters
- Efficiently check if a string has all unique characters without using any additional data structure
- Check if characters of a given string can be used to form any N equal strings
- Smallest window that contains all characters of string itself
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Longest subarray having count of 1s one more than count of 0s
- Longest subarray not having more than K distinct elements
- Distributing items when a person cannot take more than two items of same type
- Prefixes with more a than b
- Array elements that appear more than once
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.