📌  相关文章
📜  计算字符串形成的最小组数

📅  最后修改于: 2021-09-07 04:37:21             🧑  作者: Mango

给定一个包含两个或多个大写英文字母的字符串“S” 。任务是通过用相同的单个字符替换连续字符来计算完全覆盖字符串所需的最少组数。

例子:

Input : S = "TTWWW"
Output : 2
Explanation : 
There are 2 groups formed. One by covering the 2 consecutive T and the other by 3 consecutive W. 

Input : S = "FFMMMF"
Output : 3
Explanation : 
Minimum number of groups formed is 3 that is two F's, three M's and one F .
Note: Three F's were not included in one group because they are not consecutive in the string s.

方法:

解决上述问题的主要思想是将字符串’S’中的相邻字符一一比较。例如,如果字符不同,即字符串的连续字母不相同,则形成的总组的计数器递增 1,依此类推,直到达到字符串的长度。

下面是上述方法的实现:

C++
// C++ implementation to Count the
// minimum number of groups formed in a string
// by replacing consecutive characters
// with same single character
#include
  
using namespace std;
  
// Function to count the minimum number
// of groups formed in the given string s
void group_formed(string S){
  
    // Initializing count as one since
    // the string is not NULL
    int count = 1;
  
    for (int i = 0; i < S.size() - 1; i++){
  
        // Comparing adjacent characters
        if ( S[i] != S[i+1])
            count += 1;
          }
  
    cout << (count);
  }
  
// Driver Code
int main(){
    string S = "TTWWW";
  
    group_formed(S);
  }
  
// This code is contributed by mohit kumar 29


Java
// Java implementation to Count the 
// minimum number of groups formed in a string 
// by replacing consecutive characters 
// with same single character 
class GFG {
      
    // Function to count the minimum number 
    // of groups formed in the given string s 
    static void group_formed(String S){ 
      
        // Initializing count as one since 
        // the string is not NULL 
        int count = 1; 
      
        for (int i = 0; i < S.length() - 1; i++){ 
      
            // Comparing adjacent characters 
            if ( S.charAt(i) != S.charAt(i+1)) 
                count += 1; 
            } 
      
        System.out.println(count); 
    } 
      
    // Driver Code 
    public static void main (String[] args) { 
        String S = "TTWWW"; 
      
        group_formed(S); 
    } 
}
  
 // This code is contributed by AnkitRai01


Python3
# Python3 implementation to Count the
# minimum number of groups formed in a string
# by replacing consecutive characters
# with same single character
  
# Function to count the minimum number
# of groups formed in the given string s
def group_formed(S):
      
    # Initializing count as one since
    # the string is not NULL 
    count = 1
      
    for i in range (len(S)-1):
        a = S[i]
        b = S[i + 1]
          
        # Comparing adjacent characters
        if ( a != b):
              
            count += 1
              
    print (count)
  
# Driver Code 
if __name__ == "__main__": 
    S = "TTWWW"
      
    group_formed(S)


C#
// C# implementation to Count the 
// minimum number of groups formed 
// in a string by replacing consecutive 
// characters with same single character 
using System;
  
class GFG{ 
      
// Function to count the minimum number 
// of groups formed in the given string s 
static void group_formed(String S)
{ 
      
    // Initializing count as one since 
    // the string is not NULL 
    int count = 1; 
      
    for(int i = 0; i < S.Length - 1; i++)
    { 
         
       // Comparing adjacent characters 
       if (S[i] != S[i + 1]) 
           count += 1; 
    }
    Console.Write(count); 
} 
      
// Driver Code 
public static void Main (String[] args)
{ 
    String S = "TTWWW"; 
      
    group_formed(S); 
} 
} 
  
// This code is contributed by Rajnis09


输出:
2

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