📌  相关文章
📜  可以在不使用给定字符列表的情况下形成的子串数

📅  最后修改于: 2021-09-06 06:43:30             🧑  作者: Mango

给定一个字符串str和一个字符列表L ,任务是计算字符串str的子串总数,而不使用列表L 中给出的字符。

例子:

方法:给定长度为 N 的字符串的子串总数由公式给出

(N * (N + 1)) / 2

这个想法是使用上面的公式并按照以下步骤计算答案:

  1. 横越字符的字符串str字符。
  2. 计算未找到列表 L 中的字符的字符数。让计数为N
  3. 一旦遇到来自L的字符,计算(N * (N + 1) / 2)并将其添加到答案中并将计数 N 重置为零。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find the Number of sub-strings
// without using given character
int countSubstring(string& S, char L[], int& n)
{
    int freq[26] = { 0 }, ans = 0;
 
    // Mark the given characters in
    // the freq array
    for (int i = 0; i < n; i++) {
        freq[(int)(L[i] - 'a')] = 1;
    }
 
    // Count variable to store the count
    // of the characters until a character
    // from given L is encountered
    int count = 0;
 
    for (auto x : S) {
 
        // If a character from L is encountered,
        // then the answer variable is incremented by
        // the value obtained by using
        // the mentioned formula and count is set to 0
        if (freq[(int)(x - 'a')]) {
            ans += (count * count + count) / 2;
            count = 0;
        }
        else
            count++;
    }
 
    // For last remaining characters
    ans += (count * count + count) / 2;
 
    return ans;
}
 
// Driver code
int main()
{
 
    string S = "abcpxyz";
    char L[] = { 'a', 'p', 'q' };
    int n = sizeof(L) / sizeof(L[0]);
 
    cout << countSubstring(S, L, n);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
// Function to find the Number of sub-Strings
// without using given character
static int countSubString(char []S, char L[], int n)
{
    int []freq = new int[26];
    int ans = 0;
 
    // Mark the given characters in
    // the freq array
    for (int i = 0; i < n; i++)
    {
        freq[(int)(L[i] - 'a')] = 1;
    }
 
    // Count variable to store the count
    // of the characters until a character
    // from given L is encountered
    int count = 0;
 
    for (int x : S)
    {
 
        // If a character from L is encountered,
        // then the answer variable is incremented by
        // the value obtained by using
        // the mentioned formula and count is set to 0
        if (freq[(int)(x - 'a')] > 0)
        {
            ans += (count * count + count) / 2;
            count = 0;
        }
        else
            count++;
    }
 
    // For last remaining characters
    ans += (count * count + count) / 2;
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    String S = "abcpxyz";
    char L[] = { 'a', 'p', 'q' };
    int n = L.length;
 
    System.out.print(countSubString(S.toCharArray(), L, n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the above approach
 
# Function to find the Number of sub-strings
# without using given character
def countSubstring(S, L,n):
    freq = [0 for i in range(26)]
     
    # the freq array
    for i in range(n):
        freq[(ord(L[i]) - ord('a'))] = 1
 
    # Count variable to store the count
    # of the characters until a character
    # from given L is encountered
    count,ans = 0,0
 
    for x in S:
 
        # If a character from L is encountered,
        # then the answer variable is incremented by
        # the value obtained by using
        # the mentioned formula and count is set to 0
        if (freq[ord(x) - ord('a')]):
            ans += (count * count + count) // 2
            count = 0
        else:
            count += 1
 
    # For last remaining characters
    ans += (count * count + count) // 2
 
    return ans
 
# Driver code
 
S = "abcpxyz"
L = ['a', 'p', 'q']
n = len(L)
 
print(countSubstring(S, L, n))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
    // Function to find the Number of sub-Strings
    // without using given character
    static int countSubString(char []S, char []L, int n)
    {
        int []freq = new int[26];
        int ans = 0;
     
        // Mark the given characters in
        // the freq array
        for (int i = 0; i < n; i++)
        {
            freq[(int)(L[i] - 'a')] = 1;
        }
     
        // Count variable to store the count
        // of the characters until a character
        // from given L is encountered
        int count = 0;
     
        foreach (int x in S)
        {
     
            // If a character from L is encountered,
            // then the answer variable is incremented by
            // the value obtained by using
            // the mentioned formula and count is set to 0
            if (freq[(int)(x - 'a')] > 0)
            {
                ans += (count * count + count) / 2;
                count = 0;
            }
            else
                count++;
        }
     
        // For last remaining characters
        ans += (count * count + count) / 2;
     
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        string S = "abcpxyz";
        char []L = { 'a', 'p', 'q' };
        int n = L.Length;
     
        Console.WriteLine(countSubString(S.ToCharArray(), L, n));
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
9

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