Count the minimum number of groups formed in a string | C ++ 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 ; Initializing count as one since the string is not NULL ; Comparing adjacent characters ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void group_formed ( string S ) { int count = 1 ; for ( int i = 0 ; i < S . size ( ) - 1 ; i ++ ) { if ( S [ i ] != S [ i + 1 ] ) count += 1 ; } cout << ( count ) ; } int main ( ) { string S = " TTWWW " ; group_formed ( S ) ; }