Check if a String contains any index with more than K active characters
Last Updated :
11 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
string checkString(string s, int K)
{
int n = s.length();
unordered_map< char , int > mp;
for ( int i = 0; i < n; i++) {
mp[s[i]] = i;
}
int cnt = 0, f = 0;
unordered_set< int > st;
for ( int i = 0; i < n; i++) {
st.insert(s[i]);
if (st.size() > K) {
f = 1;
break ;
}
if (mp[s[i]] == i)
st.erase(s[i]);
}
return (f == 1 ? "Yes" : "No" );
}
int main()
{
string s = "aabbcdca" ;
int k = 2;
cout << checkString(s, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String checkString(String s, int K)
{
int n = s.length();
Map<Character,
Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
mp.put(s.charAt(i), i);
}
int cnt = 0 , f = 0 ;
Set<Character> st = new HashSet<>();
for ( int i = 0 ; i < n; i++)
{
st.add(s.charAt(i));
if (st.size() > K)
{
f = 1 ;
break ;
}
if (mp.get(s.charAt(i)) == i)
st.remove(s.charAt(i));
}
return (f == 1 ? "Yes" : "No" );
}
public static void main(String[] args)
{
String s = "aabbcdca" ;
int k = 2 ;
System.out.println(checkString(s, k));
}
}
|
Python3
def checkString(s, K):
n = len (s)
dict = {}
for i in range (n):
dict [s[i]] = i;
st = set ()
for i in range (n):
st.add(s[i])
if len (st) > K:
print ( "Yes" )
return
if dict [s[i]] = = i:
st.remove(s[i])
print ( "No" )
s = "aabbcdca"
K = 2
checkString(s, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static String checkString(String s, int K)
{
int n = s.Length;
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;
HashSet< char > st = new HashSet< char >();
for ( int i = 0; i < n; i++)
{
st.Add(s[i]);
if (st.Count > K)
{
f = 1;
break ;
}
if (mp[s[i]] == i)
st.Remove(s[i]);
}
return (f == 1 ? "Yes" : "No" );
}
public static void Main(String[] args)
{
String s = "aabbcdca" ;
int k = 2;
Console.WriteLine(checkString(s, k));
}
}
|
Javascript
<script>
function checkString(s, K)
{
var n = s.length;
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;
var st = new Set();
for ( var i = 0; i < n; i++) {
st.add(s[i]);
if (st.size > K) {
f = 1;
break ;
}
if (mp.get(s[i]) == i)
st. delete (s[i]);
}
return (f == 1 ? "Yes" : "No" );
}
var s = "aabbcdca" ;
var k = 2;
document.write( checkString(s, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1) because size of map and set will be constant
Share your thoughts in the comments
Please Login to comment...