Open In App

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

Last Updated : 11 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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


Javascript




<script>
 
// Javascript Program to implement
// the above approach
 
// Function to check if any index
// contains more than K active characters
function checkString(s, K)
{
 
    var n = s.length;
 
    // Store the last occurrence of
    // each character in the map.
    var mp = new Map();
 
    for (var i = 0; i < n; i++) {
        if(mp.has(s[i]))
        {
            mp.set(s[i], mp.get(s[i])+1);
        }
        else
            mp.set(s[i], 1);
    }
 
    var cnt = 0, f = 0;
 
    // Stores the active
    // characters
    var st = new Set();
 
    for (var i = 0; i < n; i++) {
 
        // Insert the character
        st.add(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.get(s[i]) == i)
            st.delete(s[i]);
    }
 
    return (f == 1 ? "Yes" : "No");
}
 
// Driver Code
var s = "aabbcdca";
var k = 2;
document.write( checkString(s, k));
 
// This code is contributed by importantly.
</script>


Output: 

Yes

Time Complexity: O(N) 
Auxiliary Space: O(1) because size of map and set will be constant



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads