📌  相关文章
📜  至少包含一次字符X 的子字符串的计数

📅  最后修改于: 2022-05-13 01:57:07.740000             🧑  作者: Mango

至少包含一次字符X 的子字符串的计数

给定一个字符串str和一个字符X 。任务是找出至少包含一次字符X的子串的总数。
例子:

方法:子串的总数为n * (n + 1) / 2 ,其中只需要计算那些包含字符X的子串。字符X存在于从X的位置到字符串长度的那些子字符串中。对于X之前的每个字符,必须计算此子字符串。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// required sub-strings
int countSubStr(string str, int n, char x)
{
    int res = 0, count = 0;
    for (int i = 0; i < n; i++) {
        if (str[i] == x) {
 
            // Number of sub-strings from position
            // of current x to the end of str
            res += ((count + 1) * (n - i));
 
            // To store the number of characters
            // before x
            count = 0;
        }
        else
            count++;
    }
 
    return res;
}
 
// Driver code
int main()
{
    string str = "abcabc";
    int n = str.length();
    char x = 'c';
 
    cout << countSubStr(str, n, x);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
// Function to return the count of
// required sub-strings
static int countSubStr(String str, int n, char x)
{
    int res = 0, count = 0;
    for (int i = 0; i < n; i++)
    {
        if (str.charAt(i) == x)
        {
 
            // Number of sub-strings from position
            // of current x to the end of str
            res += ((count + 1) * (n - i));
 
            // To store the number of characters
            // before x
            count = 0;
        }
        else
            count++;
    }
 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "abcabc";
    int n = str.length();
    char x = 'c';
 
    System.out.println(countSubStr(str, n, x));
}
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python implementation of the approach
  
# Function to return the count of
# required sub-strings
def countSubStr(str, n, x):
    res = 0; count = 0;
    for i in range(n):
        if (str[i] == x):
  
            # Number of sub-strings from position
            # of current x to the end of str
            res += ((count + 1) * (n - i));
  
            # To store the number of characters
            # before x
            count = 0;
        else:
            count+=1;
  
    return res;
 
# Driver code
str = "abcabc";
n = len(str);
x = 'c';
  
print(countSubStr(str, n, x));
 
# This code contributed by PrinciRaj1992


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the count of
// required sub-strings
static int countSubStr(String str, int n, char x)
{
    int res = 0, count = 0;
    for (int i = 0; i < n; i++)
    {
        if (str[i] == x)
        {
 
            // Number of sub-strings from position
            // of current x to the end of str
            res += ((count + 1) * (n - i));
 
            // To store the number of characters
            // before x
            count = 0;
        }
        else
            count++;
    }
 
    return res;
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "abcabc";
    int n = str.Length;
    char x = 'c';
 
    Console.WriteLine(countSubStr(str, n, x));
}
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


输出:
15