📌  相关文章
📜  仅使用给定字符集形成的子串计数

📅  最后修改于: 2021-09-07 05:16:43             🧑  作者: Mango

给定一个字符串str和一个由K 个字符的数组arr[] ,任务是从给定的字符数组arr[] 中找到只包含字符的str子串的数量。

注意:字符串strarr[]仅包含小写字母。

例子:

朴素的方法:朴素的方法是为给定的字符串str 生成所有可能的子字符串,并检查每个子字符串是否由数组arr[] 中的给定字符组成。如果是,则计算该子字符串,否则检查下一个子字符串。

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

有效的方法:可以优化上述朴素的方法,因为我们将删除由字符形成的、不存在于给定数组arr[] 中的子串的计数。以下是步骤:

  1. 将数组arr[]中存在的字符存储在大小为 26 的布尔数组中,以便可以在O(1)时间内完成对任何字符的搜索。
  2. 由长度为N的字符串形成子串的总数是(N *(N + 1))/ 2,INITIALISE计数为(N *(N + 1))/ 2。
  3. 从左到右遍历字符串并使用变量lastPos存储我们在字符串中遇到的最后一个字符的索引,该字符不存在于数组arr[] 中
  4. 如果在遍历字符串遇到任何不存在于arr[] 中的字符,那么我们减去包含该字符的子字符串的数量,并且起始点大于lastPos的值。假设我们在索引i那么要减去的子串的数量将由下式给出
    (i - lastPos)*(N - i)
    
  5. 每次遇到charArray[] 中不可用的字符,更新lastPos的值。

      下面是上述方法的实现:

      C++
      // C++ program for the above approach
      #include 
      using namespace std;
        
      // Function to find the number of
      // substrings that can be formed
      // using given characters
      void numberofsubstrings(string str, int k,
                              char charArray[])
      {
          int N = str.length();
        
          // Boolean array for storing
          // the available characters
          bool available[26] = { 0 };
        
          // Mark indices of all
          // available characters as 1
          for (int i = 0; i < k; i++) {
              available[charArray[i] - 'a'] = 1;
          }
        
          // Intialize lastPos as -1
          int lastPos = -1;
        
          // Initialize ans with the total
          // no of possible substrings
          int ans = (N * (N + 1)) / 2;
        
          // Traverse the string from
          // left to right
          for (int i = 0; i < N; i++) {
        
              // If the current character
              // is not present in B
              if (available[str[i] - 'a'] == 0) {
        
                  // Subtract the total possible
                  // substrings
                  ans -= ((i - lastPos)
                          * (N - i));
        
                  // Update the value of
                  // lastpos to current index
                  lastPos = i;
              }
          }
        
          // Print the final answer
          cout << ans << endl;
      }
        
      // Driver Code
      int main()
      {
          // Given String
          string str = "abcb";
          int k = 2;
        
          // Given character array
          char charArray[k] = { 'a', 'b' };
        
          // Function Call
          numberofsubstrings(str, k, charArray);
          return 0;
      }


      Java
      // Java program for the above approach
      import java.util.Arrays;
        
      class GFG{
            
      // Function to find the number of
      // substrings that can be formed
      // using given characters
      public static void numberofsubstrings(String str, int k,
                                            char charArray[])
      {
          int N = str.length();
        
          // Boolean array for storing
          // the available characters
          int available[] = new int[26];
          Arrays.fill(available, 0);
        
          // Mark indices of all
          // available characters as 1
          for(int i = 0; i < k; i++)
          {
             available[charArray[i] - 'a'] = 1;
          }
        
          // Initialize lastPos as -1
          int lastPos = -1;
        
          // Initialize ans with the total
          // no of possible substrings
          int ans = (N * (N + 1)) / 2;
        
          // Traverse the string from
          // left to right
          for(int i = 0; i < N; i++)
          {
               
             // If the current character
             // is not present in B
             if (available[str.charAt(i) - 'a'] == 0)
             {
                   
                 // Subtract the total possible
                 // substrings
                 ans -= ((i - lastPos) * (N - i));
                   
                 // Update the value of
                 // lastpos to current index
                 lastPos = i;
             }
          }
            
          // Print the final answer
          System.out.println(ans);
      }
        
      // Driver Code
      public static void main(String args[])
      {
            
          // Given String
          String str = "abcb";
          int k = 2;
        
          // Given character array
          char []charArray = {'a', 'b'};
        
          // Function Call
          numberofsubstrings(str, k, charArray);
      }
      }
        
      // This code is contributed by SoumikMondal


      Python3
      # Python3 program for the above approach
        
      # Function to find the number of
      # substrings that can be formed
      # using given characters
      def numberofsubstrings(str, k, charArray):
            
          N = len(str)
        
          # Boolean array for storing
          # the available characters
          available = [0] * 26
        
          # Mark indices of all
          # available characters as 1
          for i in range(0, k):
              available[ord(charArray[i]) - 
                        ord('a')] = 1
            
          # Initialize lastPos as -1
          lastPos = -1
        
          # Initialize ans with the total
          # no of possible substrings
          ans = (N * (N + 1)) / 2
        
          # Traverse the string from
          # left to right
          for i in range(0, N):
        
              # If the current character
              # is not present in B
              if (available[ord(str[i]) -
                            ord('a')] == 0):
        
                  # Subtract the total possible
                  # substrings
                  ans -= ((i - lastPos) * (N - i))
        
                  # Update the value of
                  # lastpos to current index
                  lastPos = i
                
          # Print the final answer
          print(int(ans))
        
      # Driver Code
        
      # Given String
      str = "abcb"
      k = 2
        
      # Given character array
      charArray = [ 'a', 'b' ]
        
      # Function call
      numberofsubstrings(str, k, charArray)
        
      # This code is contributed by sanjoy_62


      C#
      // C# program for the above approach
      using System;
        
      class GFG{
            
      // Function to find the number of
      // substrings that can be formed
      // using given characters
      public static void numberofsubstrings(String str, int k,
                                            char []charArray)
      {
          int N = str.Length;
        
          // Boolean array for storing
          // the available characters
          int []available = new int[26];
        
          // Mark indices of all
          // available characters as 1
          for(int i = 0; i < k; i++)
          {
             available[charArray[i] - 'a'] = 1;
          }
        
          // Initialize lastPos as -1
          int lastPos = -1;
        
          // Initialize ans with the total
          // no of possible substrings
          int ans = (N * (N + 1)) / 2;
        
          // Traverse the string from
          // left to right
          for(int i = 0; i < N; i++)
          {
               
             // If the current character
             // is not present in B
             if (available[str[i] - 'a'] == 0)
             {
        
                 // Subtract the total possible
                 // substrings
                 ans -= ((i - lastPos) * (N - i));
                   
                 // Update the value of
                 // lastpos to current index
                 lastPos = i;
             }
          }
            
          // Print the final answer
          Console.WriteLine(ans);
      }
        
      // Driver Code
      public static void Main(String []args)
      {
            
          // Given String
          String str = "abcb";
          int k = 2;
        
          // Given character array
          char []charArray = {'a', 'b'};
        
          // Function Call
          numberofsubstrings(str, k, charArray);
      }
      }
        
      // This code is contributed by Princi Singh


      输出:
      4
      

      时间复杂度: O(N) ,N 是字符串的长度
      辅助空间: O(1)

      如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live