📌  相关文章
📜  仅包含给定字符的子字符串的计数

📅  最后修改于: 2021-09-04 09:36:45             🧑  作者: Mango

给定一个字符串S和一个字符C,任务是计算S中只包含字符C的子串的数量。
例子:

天真的方法:
最简单的方法是生成给定字符串S 的所有可能子串,并计算仅包含字符C的子串
时间复杂度: O(N 3 )
空间复杂度: O(1)
有效的方法:
为了优化上述方法,可以应用长度为N的字符串形成N*(N+1)/2个子字符串的事实。因此,对于字符串连续出现N个字符C ,生成N*(N+1)/2个子串。因此,遍历字符串S的整个长度,并为字符C 的每个连续子字符串计算可能的子字符串数量,并将其添加到可能的子字符串总数中。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function that finds the count
// of substrings containing only
// character C in the string S
void countSubString(string S, char C)
{
    // To store total count
    // of substrings
    int count = 0;
 
    // To store count of
    // consecutive C's
    int conCount = 0;
 
    // Loop through the string
    for (char ch : S) {
 
        // Increase the consecutive
        // count of C's
        if (ch == C)
            conCount++;
 
        else {
 
            // Add count of sub-strings
            // from consecutive strings
            count += (conCount
                      * (conCount + 1))
                     / 2;
 
            // Reset the consecutive
            // count of C's
            conCount = 0;
        }
    }
 
    // Add count of sub-strings from
    // consecutive strings
    count += (conCount
              * (conCount + 1))
             / 2;
 
    // Print the count of sub-strings
    // containing only C
    cout << count;
}
 
// Driver Code
int main()
{
    string S = "geeksforgeeks";
 
    char C = 'e';
 
    countSubString(S, C);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function that finds the count
// of substrings containing only
// character C in the string S
static void countSubString(String S, char C)
{
     
    // To store total count
    // of substrings
    int count = 0;
 
    // To store count of
    // consecutive C's
    int conCount = 0;
 
    // Loop through the string
    for(int i = 0; i < S.length(); i++)
    {
        char ch = S.charAt(i);
         
        // Increase the consecutive
        // count of C's
        if (ch == C)
            conCount++;
 
        else
        {
             
            // Add count of sub-strings
            // from consecutive strings
            count += (conCount *
                     (conCount + 1)) / 2;
 
            // Reset the consecutive
            // count of C's
            conCount = 0;
        }
    }
 
    // Add count of sub-strings from
    // consecutive strings
    count += (conCount *
             (conCount + 1)) / 2;
 
    // Print the count of sub-strings
    // containing only C
    System.out.println(count);
}
 
// Driver Code
public static void main(String s[])
{
    String S = "geeksforgeeks";
    char C = 'e';
     
    countSubString(S, C);
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program to implement
# the above approach
 
# Function that finds the count
# of substrings containing only
# character C in the S
def countSubString(S, C):
 
    # To store total count
    # of substrings
    count = 0
 
    # To store count of
    # consecutive C's
    conCount = 0
 
    # Loop through the string
    for ch in S:
 
        # Increase the consecutive
        # count of C's
        if (ch == C):
            conCount += 1
             
        else:
 
            # Add count of sub-strings
            # from consecutive strings
            count += ((conCount *
                      (conCount + 1)) // 2)
 
            # Reset the consecutive
            # count of C's
            conCount = 0
 
    # Add count of sub-strings from
    # consecutive strings
    count += ((conCount *
              (conCount + 1)) // 2)
 
    # Print the count of sub-strings
    # containing only C
    print(count)
 
# Driver Code
if __name__ == '__main__':
   
    S = "geeksforgeeks"
    C = 'e'
 
    countSubString(S, C)
     
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach 
using System;
 
class GFG{
 
// Function that finds the count
// of substrings containing only
// character C in the string S
static void countSubString(String S, char C)
{
 
    // To store total count
    // of substrings
    int count = 0;
 
    // To store count of
    // consecutive C's
    int conCount = 0;
 
    // Loop through the string
    for(int i = 0; i < S.Length; i++)
    {
        char ch = S[i];
 
        // Increase the consecutive
        // count of C's
        if (ch == C)
            conCount++;
 
        else
        {
             
            // Add count of sub-strings
            // from consecutive strings
            count += (conCount *
                     (conCount + 1)) / 2;
 
            // Reset the consecutive
            // count of C's
            conCount = 0;
        }
    }
 
    // Add count of sub-strings from
    // consecutive strings
    count += (conCount *
             (conCount + 1)) / 2;
 
    // Print the count of sub-strings
    // containing only C
    Console.Write(count);
}
 
// Driver Code
public static void Main(String[] args)
{
    String S = "geeksforgeeks";
    char C = 'e';
 
    countSubString(S, C);
}
}
 
// This code is contributed by grand_master


Javascript


输出:
6

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

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