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

📅  最后修改于: 2021-04-26 17:52:11             🧑  作者: Mango

给定一个字符串str和一个字符c 。任务是查找不包含字符c的子字符串的数量。

例子:

方法:最初使用一个不带字符c的连续计数字符数的计数器。迭代字符串并增加计数器,直到str [i]!= c 。一旦str [i] == c ,连续长度cnt的子字符串数将为(cnt *(cnt + 1))/ 2 。在字符串的完全遍历之后,还将(cnt *(cnt + 1))/ 2添加到最后一次出现c之后出现的字符组的结果中。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the number
// of sub-strings that do not contain
// the given character c
int countSubstrings(string s, char c)
{
  
    // Length of the string
    int n = s.length();
  
    int cnt = 0;
    int sum = 0;
  
    // Traverse in the string
    for (int i = 0; i < n; i++) {
  
        // If current character is different
        // from the given character
        if (s[i] != c)
            cnt++;
        else {
  
            // Update the number of sub-strings
            sum += (cnt * (cnt + 1)) / 2;
  
            // Reset count to 0
            cnt = 0;
        }
    }
  
    // For the characters appearing
    // after the last occurrence of c
    sum += (cnt * (cnt + 1)) / 2;
    return sum;
}
  
// Driver code
int main()
{
    string s = "baa";
    char c = 'b';
    cout << countSubstrings(s, c);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function to return the number
// of sub-strings that do not contain
// the given character c
static int countSubstrings(String s, char c)
{
  
    // Length of the string
    int n = s.length();
  
    int cnt = 0;
    int sum = 0;
  
    // Traverse in the string
    for (int i = 0; i < n; i++)
    {
  
        // If current character is different
        // from the given character
        if (s.charAt(i) != c)
            cnt++;
        else
        {
  
            // Update the number of sub-strings
            sum += (cnt * (cnt + 1)) / 2;
  
            // Reset count to 0
            cnt = 0;
        }
    }
  
    // For the characters appearing
    // after the last occurrence of c
    sum += (cnt * (cnt + 1)) / 2;
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    String s = "baa";
    char c = 'b';
    System.out.println(countSubstrings(s, c));
}
}
  
// This code is contributed by Code_Mech.


Python3
# Python3 implementation of the approach
  
# Function to return the number
# of sub-strings that do not contain
# the given character c
def countSubstrings(s, c):
  
    # Length of the string
    n = len(s)
  
    cnt = 0
    Sum = 0
  
    # Traverse in the string
    for i in range(n):
  
        # If current character is different
        # from the given character
        if (s[i] != c):
            cnt += 1
        else:
  
            # Update the number of sub-strings
            Sum += (cnt * (cnt + 1)) // 2
  
            # Reset count to 0
            cnt = 0
          
    # For the characters appearing
    # after the last occurrence of c
    Sum += (cnt * (cnt + 1)) // 2
    return Sum
  
# Driver code
s = "baa"
c = 'b'
print(countSubstrings(s, c))
  
# This code is contributed 
# by mohit kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the number
// of sub-strings that do not contain
// the given character c
static int countSubstrings(string s, char c)
{
  
    // Length of the string
    int n = s.Length;
  
    int cnt = 0;
    int sum = 0;
  
    // Traverse in the string
    for (int i = 0; i < n; i++)
    {
  
        // If current character is different
        // from the given character
        if (s[i] != c)
            cnt++;
        else
        {
  
            // Update the number of sub-strings
            sum += (cnt * (cnt + 1)) / 2;
  
            // Reset count to 0
            cnt = 0;
        }
    }
  
    // For the characters appearing
    // after the last occurrence of c
    sum += (cnt * (cnt + 1)) / 2;
    return sum;
}
  
// Driver code
public static void Main()
{
    string s = "baa";
    char c = 'b';
    Console.Write(countSubstrings(s, c));
}
}
  
// This code is contributed by Akanksha Rai


PHP


输出:
3