📌  相关文章
📜  计算由单个不同字符组成的子串

📅  最后修改于: 2021-09-02 05:59:45             🧑  作者: Mango

给定长度为N的字符串S ,任务是计算由单个不同字符组成的子字符串的数量。
注意:对于相同子串的重复出现,计算所有重复。

例子:

朴素的方法:解决这个问题的最简单的方法是从给定的字符串中生成所有子字符串,计算仅由单个不同字符组成的子字符串的数量。
时间复杂度: O(N 3 )
辅助空间: O(1)

有效方法:按照以下步骤优化上述方法:

  • 初始化一个变量,例如ans ,以存储此类子字符串的计数。
  • 遍历字符串并检查的字符,如果前一个字符,说,是与当前字符或没有。
  • 虽然发现前一个和当前字符相同,但增加subs并添加到答案中。
  • 如果发现前一个字符和当前字符不同,则将subs重新初始化为1

下面是上述方法的实现:

C++
#include 
using namespace std;
 
  // Function to count the number
  // of substrings made up of a
  // single distinct character
  void countSubstrings(string s)
  {
     
    // Stores the required count
    int ans = 0;
 
    // Stores the count of substrings
    // possible by using current character
    int subs = 1;
 
    // Stores the previous character
    char pre = '0';
 
    // Traverse the string
    for (auto& i : s)
    {
       
      // If current character is same
      // as the previous character
      if(pre == i)
      {
         
        // Increase count of substrings
        // possible with current character
        subs += 1;
      }
      else
      {
         
        // Reset count  of substrings
        // possible with current character
        subs = 1;
      }
 
      // Update count of substrings
      ans += subs;
 
      // Update previous character
      pre = i;
    }
    cout << ans <


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to count the number
  // of substrings made up of a
  // single distinct character
  static void countSubstrings(String s)
  {
     
    // Stores the required count
    int ans = 0;
 
    // Stores the count of substrings
    // possible by using current character
    int subs = 1;
 
    // Stores the previous character
    char pre = '0';
 
    // Traverse the string
    for(char i : s.toCharArray())
    {
       
      // If current character is same
      // as the previous character
      if(pre == i)
      {
         
        // Increase count of substrings
        // possible with current character
        subs += 1;
      }
      else
      {
         
        // Reset count  of substrings
        // possible with current character
        subs = 1;
      }
 
      // Update count of substrings
      ans += subs;
 
      // Update previous character
      pre = i;
    }
    System.out.println(ans);
  }
 
// Driver Code
public static void main(String[] args)
{
    String s = "geeksforgeeks";
    countSubstrings(s);
}
}
 
// This code is contributed by souravghosh0416.


Python3
# Python3 Program to
# implement the above approach
 
# Function to count the number
# of substrings made up of a
# single distinct character
 
 
def countSubstrings(s):
 
    # Stores the required count
    ans = 0
 
    # Stores the count of substrings
    # possible by using current character
    subs = 1
 
    # Stores the previous character
    pre = ''
 
    # Traverse the string
    for i in s:
 
        # If current character is same
        # as the previous character
        if pre == i:
 
            # Increase count of substrings
            # possible with current character
            subs += 1
        else:
 
            # Reset count  of substrings
            # possible with current character
            subs = 1
 
        # Update count of substrings
        ans += subs
 
        # Update previous character
        pre = i
 
    print(ans)
 
 
# Driver Code
s = 'geeksforgeeks'
countSubstrings(s)


C#
// C# Program to
// implement the above approach
using System;
class GFG {
 
  // Function to count the number
  // of substrings made up of a
  // single distinct character
  static void countSubstrings(string s)
  {
     
    // Stores the required count
    int ans = 0;
 
    // Stores the count of substrings
    // possible by using current character
    int subs = 1;
 
    // Stores the previous character
    char pre = '0';
 
    // Traverse the string
    foreach(char i in s)
    {
       
      // If current character is same
      // as the previous character
      if(pre == i)
      {
         
        // Increase count of substrings
        // possible with current character
        subs += 1;
      }
      else
      {
         
        // Reset count  of substrings
        // possible with current character
        subs = 1;
      }
 
      // Update count of substrings
      ans += subs;
 
      // Update previous character
      pre = i;
    }
    Console.WriteLine(ans);
  }
 
  // Driver code
  static void Main() {
    string s = "geeksforgeeks";
    countSubstrings(s);
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:
15

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

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