📌  相关文章
📜  计算以字符X 开头并以字符Y 结尾的子字符串

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

计算以字符X 开头并以字符Y 结尾的子字符串

给定一个包含n个小写字符的字符串str ,任务是计算str中以字符X 开始并以字符Y 结束的子串的数量。
例子:

Input: str = "abbcaceghcak"
        x = 'a', y = 'c'
Output: 5
abbc, abbcac, ac, abbcaceghc, aceghc

Input: str = "geeksforgeeks"
        x = 'g', y = 'e'
Output: 6

方法:

  • 初始化两个计数器,即 tot_count 来统计子字符串的总数,count_x 来统计以 X 开头的字符串的数量。
  • 开始遍历字符串。
  • 如果当前字符是 X,则增加 count_x 的计数。
  • 如果当前字符为 Y,则表示字符串以 Y 结尾,因此增加 tot_count 的计数,即
tot_count = tot_count + count_x
  • 这意味着如果存在 Y 那么它将生成一个子字符串,其中所有 X 出现在字符串中的 Y 之前。因此,将 X 的计数添加到总计数中。
  • 返回总计数。

以下是上述方法的实现:

C++
// C++ implementation to count substrings
// starting with character X and ending
// with character Y
#include 
using namespace std;
 
// function to count substrings starting with
// character X and ending with character Y
int countSubstr(string str, int n,
                char x, char y)
{
    // to store total count of
    // required substrings
    int tot_count = 0;
 
    // to store count of character 'x'
    // up to the point the string 'str'
    // has been traversed so far
    int count_x = 0;
 
    // traverse 'str' form left to right
    for (int i = 0; i < n; i++) {
 
        // if true, increment 'count_x'
        if (str[i] == x)
            count_x++;
 
        // if true accumulate 'count_x'
        // to 'tot_count'
        if (str[i] == y)
            tot_count += count_x;
    }
 
    // required count
    return tot_count;
}
 
// Driver code
int main()
{
    string str = "abbcaceghcak";
    int n = str.size();
    char x = 'a', y = 'c';
 
    cout << "Count = "
         << countSubstr(str, n, x, y);
 
    return 0;
}


Java
// Java implementation to count
// substrings starting with
// character X and ending
// with character Y
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
// function to count substrings
// starting with character X and
// ending with character Y
static int countSubstr(String str, int n,
                       char x, char y)
{
    // to store total count of
    // required substrings
    int tot_count = 0;
 
    // to store count of character
    // 'x' up to the point the
    // string 'str' has been
    // traversed so far
    int count_x = 0;
 
    // traverse 'str' form
    // left to right
    for (int i = 0; i < n; i++)
    {
 
        // if true, increment 'count_x'
        if (str.charAt(i) == x)
            count_x++;
 
        // if true accumulate 'count_x'
        // to 'tot_count'
        if (str.charAt(i) == y)
            tot_count += count_x;
    }
 
    // required count
    return tot_count;
}
 
// Driver code
public static void main(String args[])
{
    String str = "abbcaceghcak";
    int n = str.length();
    char x = 'a', y = 'c';
 
    System.out.print ("Count = " +
       countSubstr(str, n, x, y));
}
}
 
// This code is contributed
// by Subhadeep


Python3
# Python 3 implementation to count substrings
# starting with character X and ending
# with character Y
 
# function to count substrings starting with
# character X and ending with character Y
def countSubstr(str, n, x, y):
 
    # to store total count of
    # required substrings
    tot_count = 0
 
    # to store count of character 'x'
    # up to the point the string 'str'
    # has been traversed so far
    count_x = 0
 
    # traverse 'str' form left to right
    for i in range(n):
 
        # if true, increment 'count_x'
        if str[i] == x:
            count_x += 1
 
        # if true accumulate 'count_x'
        # to 'tot_count'
        if str[i] == y:
            tot_count += count_x
     
    # required count
    return tot_count
 
# Driver Code
str = 'abbcaceghcak'
n = len(str)
x, y = 'a', 'c'
print('Count =', countSubstr(str, n, x, y))
 
# This code is contributed SamyuktaSHegde


C#
// C# implementation to count substrings
// starting with character X and ending
// with character Y
using System;
 
class GFG
{
// function to count substrings starting
// with character X and ending with character Y
static int countSubstr(string str, int n,
                       char x, char y)
{
    // to store total count of
    // required substrings
    int tot_count = 0;
 
    // to store count of character 'x' up
    // to the point the string 'str' has
    // been traversed so far
    int count_x = 0;
 
    // traverse 'str' form left to right
    for (int i = 0; i < n; i++)
    {
 
        // if true, increment 'count_x'
        if (str[i] == x)
            count_x++;
 
        // if true accumulate 'count_x'
        // to 'tot_count'
        if (str[i] == y)
            tot_count += count_x;
    }
 
    // required count
    return tot_count;
}
 
// Driver code
public static void Main()
{
    string str = "abbcaceghcak";
    int n = str.Length;
    char x = 'a', y = 'c';
 
    Console.Write("Count = " +
    countSubstr(str, n, x, y));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
Count = 5