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

📅  最后修改于: 2021-04-24 18:14:17             🧑  作者: Mango

给定字符串S和字符C,任务是计算仅包含字符CS子字符串的数量。
例子:

天真的方法:
最简单的方法是生成给定字符串S的所有可能的子字符串,并对仅包含字符C的子字符串进行计数
时间复杂度: O(N 3 )
空间复杂度: O(1)
高效方法:
为了优化上述方法,可以应用长度为N的字符串形成N *(N + 1)/ 2个子字符串的事实。因此,对于字符串中字符C的N次连续出现,将生成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


输出:
6

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