Check if a String contains any index with more than K active characters

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : offbeat