📌  相关文章
📜  检查字符串是否包含超过 K 个活动字符的任何索引

📅  最后修改于: 2021-10-27 16:46:31             🧑  作者: Mango

给定一个包含小写英文字母的字符串S和一个整数K ,任务是找到包含超过K 个活动字符的字符串的任何索引。如果找到,则打印Yes 。否则,打印No

例子:

方法:
请按照以下步骤解决问题:

  • 这个想法是将字符串中出现的每个字符的最后一次出现存储在 Map 中。
  • 遍历字符串并保持将活动字母存储在一个 Set 中。
  • 如果在任何索引处, Set的大小超过K ,则打印“是”
  • 否则,检查当前索引是否是当前字符的最后一次出现。如果是,请从Set 中删除该字符。
  • 最后,如果没有找到超过K 个活动字符的索引,则打印“No”

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
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 mp;
 
    for (int i = 0; i < n; i++) {
        mp[s[i]] = i;
    }
 
    int cnt = 0, f = 0;
 
    // Stores the active
    // characters
    unordered_set 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 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 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 mp = new Dictionary();
 
    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 st = new HashSet();
 
    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


输出:
Yes

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程