📌  相关文章
📜  不同时包含集合{‘a’,’b’,’c’}中的所有字符的子字符串计数

📅  最后修改于: 2021-04-21 23:45:23             🧑  作者: Mango

给定仅由字符‘a’‘b’‘c’组成的字符串str ,找到不同时包含所有三个字符的子字符串的数量。

例子:

方法:想法是使用三个变量a_indexb_indexc_index来存储字符a,b和c的最新出现,以及另一个变量ans来存储不具有a,b或a中至少一个的子字符串的数量c并将其值初始化为长度为n的子字符串总数,即n *(n + 1)/ 2 ,其中n是字符串的长度。

现在,只需从头开始遍历字符串即可。每当遇到变量时,都将变量的值更新为最新值。由于索引为0,因此在每一步将索引更新为i + 1

同样,在每个步骤中,您都需要找到当前步骤中未遇到的两个字符其余字符的最小出现索引。

然后只需从变量ans中减去该索引即可。这是因为一旦找到了剩余字符最少的索引,就可以确保通过在索引中向下移动形成的更多子字符串也将包含所有这三个字符。

因此,在此步骤中形成的所有此类子字符串(包含所有a,b和c的子字符串)的总数就是找到的索引。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of valid sub-strings
int CountSubstring(char str[], int n)
{
  
    // Variable ans to store all the possible substrings
    // Initialize its value as total number of substrings
    // that can be formed from the given string
    int ans = (n * (n + 1)) / 2;
  
    // Stores recent index of the characters
    int a_index = 0;
    int b_index = 0;
    int c_index = 0;
  
    for (int i = 0; i < n; i++) {
  
        // If character is a update a's index
        // and the variable ans
        if (str[i] == 'a') {
            a_index = i + 1;
            ans -= min(b_index, c_index);
        }
  
        // If character is b update b's index
        // and the variable ans
        else if (str[i] == 'b') {
            b_index = i + 1;
            ans -= min(a_index, c_index);
        }
  
        // If character is c update c's index
        // and the variable ans
        else {
            c_index = i + 1;
            ans -= min(a_index, b_index);
        }
    }
  
    return ans;
}
  
// Driver code
int main()
{
    char str[] = "babac";
    int n = strlen(str);
  
    cout << CountSubstring(str, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
    // Function to return the count of valid sub-strings
    static int CountSubstring(char str[], int n) 
    {
  
        // Variable ans to store all the possible substrings
        // Initialize its value as total number of substrings
        // that can be formed from the given string
        int ans = (n * (n + 1)) / 2;
  
        // Stores recent index of the characters
        int a_index = 0;
        int b_index = 0;
        int c_index = 0;
  
        for (int i = 0; i < n; i++) 
        {
  
            // If character is a update a's index
            // and the variable ans
            if (str[i] == 'a') 
            {
                a_index = i + 1;
                ans -= Math.min(b_index, c_index);
            } 
            // If character is b update b's index
            // and the variable ans
            else if (str[i] == 'b')
            {
                b_index = i + 1;
                ans -= Math.min(a_index, c_index);
            } 
            // If character is c update c's index
            // and the variable ans
            else 
            {
                c_index = i + 1;
                ans -= Math.min(a_index, b_index);
            }
        }
  
        return ans;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        char str[] = "babac".toCharArray();
        int n = str.length;
  
        System.out.println(CountSubstring(str, n));
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function to return the count of 
# valid sub-Strings
def CountSubString(Str, n):
  
    # Variable ans to store all the possible subStrings
    # Initialize its value as total number of subStrings
    # that can be formed from the given String
    ans = (n * (n + 1)) // 2
  
    # Stores recent index of the characters
    a_index = 0
    b_index = 0
    c_index = 0
  
    for i in range(n):
  
        # If character is a update a's index
        # and the variable ans
        if (Str[i] == 'a'):
            a_index = i + 1
            ans -= min(b_index, c_index)
  
        # If character is b update b's index
        # and the variable ans
        elif (Str[i] == 'b'):
            b_index = i + 1
            ans -= min(a_index, c_index)
          
        # If character is c update c's index
        # and the variable ans
        else:
            c_index = i + 1
            ans -= min(a_index, b_index)
  
    return ans
  
# Driver code
Str = "babac"
n = len(Str)
  
print(CountSubString(Str, n))
  
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to return the count of
// valid sub-strings
static int CountSubstring(char []str, int n) 
{
  
    // Variable ans to store all the possible substrings
    // Initialize its value as total number of substrings
    // that can be formed from the given string
    int ans = (n * (n + 1)) / 2;
  
    // Stores recent index of the characters
    int a_index = 0;
    int b_index = 0;
    int c_index = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // If character is a update a's index
        // and the variable ans
        if (str[i] == 'a') 
        {
            a_index = i + 1;
            ans -= Math.Min(b_index, c_index);
        } 
        // If character is b update b's index
        // and the variable ans
        else if (str[i] == 'b')
        {
            b_index = i + 1;
            ans -= Math.Min(a_index, c_index);
        } 
        // If character is c update c's index
        // and the variable ans
        else
        {
            c_index = i + 1;
            ans -= Math.Min(a_index, b_index);
        }
    }
  
    return ans;
}
  
// Driver code
public static void Main()
{
    char []str = "babac".ToCharArray();
    int n = str.Length;
  
    Console.WriteLine(CountSubstring(str, n));
}
}
  
// This code contributed 
// by Akanksha Rai


PHP


输出:
12