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

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

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

例子:

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

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

  • 初始化一个变量,例如ans ,以存储此类子字符串的计数。
  • 遍历字符串的字符,并检查前一个字符(例如pre )是否与当前字符相同。
  • 虽然以前和当前字符被认为是相同的,增加潜艇并添加到答案。
  • 如果发现先前字符和当前字符不同,则将subs重新初始化为1

下面是上述方法的实现:

C++
#include 
using namespace std;
 
  // Function to count the number
  // of substrings made up of a
  // single distinct charcater
  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 charcater
  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 charcater
 
 
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 charcater
  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.


输出:
15

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