📌  相关文章
📜  计算最多k次包含某些字符的不同子字符串

📅  最后修改于: 2021-04-23 16:22:36             🧑  作者: Mango

给定一个整数k和一个sting str ,任务是计算不同的子字符串的数量,以使每个子字符串不包含某些特定字符超过k次。特定字符作为另一个字符串给出。

例子:

方法:

  • 将anotherStr的字符存储在大小为256的布尔数组中以快速查找
  • 遍历给定字符串的所有子字符串。对于每个子字符串,请将非法字符计数保留在anotherStr中
  • 如果这些字符的数量超过k的值,则跳出内循环。
  • 否则,将此子字符串存储在哈希表中以保留不同的子字符串。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int MAX_CHAR = 256;
  
// Function to return the count of valid sub-strings
int countSubStrings(string s, string anotherStr, int k)
{
    // Store all characters of anotherStr in a 
    // direct index table for quick lookup.
    bool illegal[MAX_CHAR] = { false };
    for (int i = 0; i < anotherStr.size(); i++)
        illegal[anotherStr[i]] = true;
  
    // To store distinct output substrings
    unordered_set us;
  
    // Traverse through the given string and
    // one by one generate substrings beginning
    // from s[i].
    for (int i = 0; i < s.size(); ++i) {
  
        // One by one generate substrings ending
        // with s[j]
        string ss = "";
        int count = 0;
        for (int j = i; j < s.size(); ++j) {
  
            // If character is illegal
            if (illegal[s[j]])
                ++count;
            ss = ss + s[j];
  
            // If current substring is valid
            if (count <= k) {
                us.insert(ss);
            }
  
            // If current substring is invalid,
            // adding more characters would not
            // help.
            else
                break;
        }
    }
  
    // Return the count of distinct sub-strings
    return us.size();
}
  
// Driver code
int main()
{
    string str = "acbacbacaa";
    string anotherStr = "abcdefghijklmnopqrstuvwxyz";
    int k = 2;
    cout << countSubStrings(str, anotherStr, k);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG {
  
    static int MAX_CHAR = 256;
  
    // Function to return the count of valid sub-strings
    static int countSubStrings(String s, String anotherStr, int k) 
    {
        // Store all characters of anotherStr in a 
        // direct index table for quick lookup.
        boolean illegal[] = new boolean[MAX_CHAR];
        for (int i = 0; i < anotherStr.length(); i++) 
        {
            illegal[anotherStr.charAt(i)] = true;
        }
  
        // To store distinct output substrings
        HashSet us = new HashSet();
  
        // Traverse through the given string and
        // one by one generate substrings beginning
        // from s[i].
        for (int i = 0; i < s.length(); ++i) 
        {
  
            // One by one generate substrings ending
            // with s[j]
            String ss = "";
            int count = 0;
            for (int j = i; j < s.length(); ++j) 
            {
  
                // If character is illegal
                if (illegal[s.charAt(j)])
                {
                    ++count;
                }
                ss = ss + s.charAt(j);
  
                // If current substring is valid
                if (count <= k) 
                {
                    us.add(ss);
                } 
                  
                // If current substring is invalid,
                // adding more characters would not
                // help.
                else 
                {
                    break;
                }
            }
        }
  
        // Return the count of distinct sub-strings
        return us.size();
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        String str = "acbacbacaa";
        String anotherStr = "abcdefghijklmnopqrstuvwxyz";
        int k = 2;
        System.out.println(countSubStrings(str, anotherStr, k));
    }
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach 
  
MAX_CHAR = 256
  
# Function to return the count
# of valid sub-strings 
def countSubStrings(s, anotherStr, k) :
      
    # Store all characters of anotherStr in
    # a direct index table for quick lookup. 
    illegal = [False] * MAX_CHAR
      
    for i in range(len(anotherStr)) :
        illegal[ord(anotherStr[i])] = True
          
    # To store distinct output substrings 
    us = set() 
      
    # Traverse through the given string 
    # and one by one generate substrings 
    # beginning from s[i]. 
    for i in range(len(s)) :
          
        # One by one generate substrings 
        # ending with s[j] 
        ss = "" 
          
        count = 0
        for j in range(i, len(s)) :
              
            # If character is illegal
            if (illegal[ord(s[j])]) :
                count += 1
            ss = ss + s[j]
              
            # If current substring is valid
            if (count <= k) :
                us.add(ss)
              
            # If current substring is invalid,
            # adding more characters would not
            # help.
            else :
                break
      
    # Return the count of distinct
    # sub-strings 
    return len(us)
  
# Driver code 
if __name__ == "__main__" : 
  
    string = "acbacbacaa"
    anotherStr = "abcdefghijklmnopqrstuvwxyz"
    k = 2
    print(countSubStrings(string, 
                          anotherStr, k))
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
    static int MAX_CHAR = 256;
  
    // Function to return the count 
    // of valid sub-strings
    static int countSubStrings(String s, 
                        String anotherStr, int k) 
    {
          
        // Store all characters of anotherStr in a 
        // direct index table for quick lookup.
        bool []illegal = new bool[MAX_CHAR];
        for (int i = 0; i < anotherStr.Length; i++) 
        {
            illegal[anotherStr[i]] = true;
        }
  
        // To store distinct output substrings
        HashSet us = new HashSet();
  
        // Traverse through the given 
        // string and one by one generate 
        // substrings beginning from s[i].
        for (int i = 0; i < s.Length; ++i) 
        {
  
            // One by one generate substrings 
            // ending with s[j]
            String ss = "";
            int count = 0;
            for (int j = i; j < s.Length; ++j) 
            {
  
                // If character is illegal
                if (illegal[s[j]])
                {
                    ++count;
                }
                ss = ss + s[j];
  
                // If current substring is valid
                if (count <= k) 
                {
                    us.Add(ss);
                } 
                  
                // If current substring is invalid,
                // adding more characters would not
                // help.
                else
                {
                    break;
                }
            }
        }
  
        // Return the count of distinct sub-strings
        return us.Count;
    }
  
    // Driver code
    public static void Main() 
    {
        String str = "acbacbacaa";
        String anotherStr = "abcdefghijklmnopqrstuvwxyz";
        int k = 2;
        Console.WriteLine(countSubStrings(str, anotherStr, k));
    }
}
  
//This code is contributed by 29AjayKumar


输出:
8