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.
Examples:
Input: S = “aabbcd”, K = 1
Output: No
Explanation:
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
Output: Yes
Explanation:
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.
Approach:
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:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to check if any index // contains more than K active characters string checkString(string s, int K) { int n = s.length(); // Store the last occurrence of // each character in the map. unordered_map<char, int> mp; for (int i = 0; i < n; i++) { mp[s[i]] = i; } int cnt = 0, f = 0; // Stores the active // characters unordered_set<int> st; for (int i = 0; i < n; i++) { // Insert the character st.insert(s[i]); // If the size of set // exceeds K if (st.size() > K) { f = 1; break; } // Remove the character from // set if i is the last index // of the current character if (mp[s[i]] == i) st.erase(s[i]); } return (f == 1 ? "Yes" : "No"); } // Driver Code int main() { string s = "aabbcdca"; int k = 2; cout << checkString(s, k); return 0; } |
Java
// Java program to implement the // above approach import java.util.*; class GFG{ // Function to check if any index // contains more than K active characters static String checkString(String s, int K) { int n = s.length(); // Store the last occurrence of // each character in the map. Map<Character, Integer> mp = new HashMap<>(); for(int i = 0; i < n; i++) { mp.put(s.charAt(i), i); } int cnt = 0, f = 0; // Stores the active // characters Set<Character> st = new HashSet<>(); for(int i = 0; i < n; i++) { // Insert the character st.add(s.charAt(i)); // If the size of set // exceeds K if (st.size() > K) { f = 1; break; } // Remove the character from // set if i is the last index // of the current character if (mp.get(s.charAt(i)) == i) st.remove(s.charAt(i)); } return (f == 1 ? "Yes" : "No"); } // Driver code public static void main(String[] args) { String s = "aabbcdca"; int k = 2; System.out.println(checkString(s, k)); } } // This code is contributed by offbeat |
Python3
# Python3 program to implement # the above approach # Function to check if any index # contains more than K active characters def checkString(s, K): n = len(s) # Store the last occurrence of # each character dict = {} for i in range(n): dict[s[i]] = i; # Stores the active # characters st = set() for i in range(n): # Insert the character st.add(s[i]) # If the size of set # exceeds K if len(st) > K: print("Yes") return # Remove the character from # set if i is the last index # of the current character if dict[s[i]] == i: st.remove(s[i]) print("No") # Driver code s = "aabbcdca"K = 2 checkString(s, K) # This code is contributed by vashisthamadhur2 |
C#
// C# program to implement the // above approach using System; using System.Collections.Generic; class GFG{ // Function to check if any index // contains more than K active characters static String checkString(String s, int K) { int n = s.Length; // Store the last occurrence of // each character in the map. Dictionary<char, int> mp = new Dictionary<char, int>(); for(int i = 0; i < n; i++) { if(mp.ContainsKey(s[i])) mp[s[i]] = i; else mp.Add(s[i], i); } int f = 0; // Stores the active // characters HashSet<char> st = new HashSet<char>(); for(int i = 0; i < n; i++) { // Insert the character st.Add(s[i]); // If the size of set // exceeds K if (st.Count > K) { f = 1; break; } // Remove the character from // set if i is the last index // of the current character if (mp[s[i]] == i) st.Remove(s[i]); } return (f == 1 ? "Yes" : "No"); } // Driver code public static void Main(String[] args) { String s = "aabbcdca"; int k = 2; Console.WriteLine(checkString(s, k)); } } // This code is contributed by shikhasingrajput |
Yes
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.
Recommended Posts:
- 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 contribute@geeksforgeeks.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.

