# Count the minimum number of groups formed in a string

Given a string ‘S’ which comprises two or more uppercase English letters. The task is to count the minimum number of groups required to completely cover the string by replacing consecutive characters with the same single character.

Examples:

```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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

To solve the problem mentioned above the main idea is to compare the adjacent characters in the string ‘S’ one by one. If for instance, the characters are different that is the consecutive letters of the string are not the same then, the counter for the total group formed is incremented by 1 and so on until we reach the length if the string.

Below is the implementation of the above-mentioned approach:

## 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  `

Output:

```2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.