Count the minimum number of groups formed in a string
Last Updated :
18 Sep, 2023
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.
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++
#include<bits/stdc++.h>
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);
}
|
Java
class GFG {
static void group_formed(String S){
int count = 1 ;
for ( int i = 0 ; i < S.length() - 1 ; i++){
if ( S.charAt(i) != S.charAt(i+ 1 ))
count += 1 ;
}
System.out.println(count);
}
public static void main (String[] args) {
String S = "TTWWW" ;
group_formed(S);
}
}
|
Python3
def group_formed(S):
count = 1
for i in range ( len (S) - 1 ):
a = S[i]
b = S[i + 1 ]
if ( a ! = b):
count + = 1
print (count)
if __name__ = = "__main__" :
S = "TTWWW"
group_formed(S)
|
C#
using System;
class GFG{
static void group_formed(String S)
{
int count = 1;
for ( int i = 0; i < S.Length - 1; i++)
{
if (S[i] != S[i + 1])
count += 1;
}
Console.Write(count);
}
public static void Main (String[] args)
{
String S = "TTWWW" ;
group_formed(S);
}
}
|
Javascript
<script>
function group_formed(S)
{
let count = 1;
for (let i = 0; i < S.length - 1; i++)
{
if (S[i] != S[i + 1])
count += 1;
}
document.write(count);
}
let S = "TTWWW" ;
group_formed(S);
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Approach:
- We can use stack to solve this problem.
- Traverse the given string character by character and push each character onto the stack if it is not the same as the top of the stack.
- If it is the same as the top of the stack, we can pop the top element of the stack and push the new character onto the stack.
- At the end, the size of the stack will give us the minimum number of groups required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minGroups(string s) {
stack< char > st;
int n = s.size();
for ( int i = 0; i < n; i++) {
if (st.empty() || s[i] != st.top()) {
st.push(s[i]);
} else {
st.pop();
st.push(s[i]);
}
}
return st.size();
}
int main() {
string s = "TTWWW" ;
cout << minGroups(s) << endl;
return 0;
}
|
Java
import java.util.Stack;
public class GFG {
static int minGroups(String s) {
Stack<Character> stack = new Stack<>();
int n = s.length();
for ( int i = 0 ; i < n; i++) {
if (stack.isEmpty() || s.charAt(i) != stack.peek()) {
stack.push(s.charAt(i));
} else {
stack.pop();
stack.push(s.charAt(i));
}
}
return stack.size();
}
public static void main(String[] args) {
String s = "TTWWW" ;
System.out.println(minGroups(s));
}
}
|
Python3
def minGroups(s):
st = []
n = len (s)
for i in range (n):
if not st or s[i] ! = st[ - 1 ]:
st.append(s[i])
else :
st.pop()
st.append(s[i])
return len (st)
def main():
s = "TTWWW"
print (minGroups(s))
if __name__ = = "__main__" :
main()
|
C#
using System;
using System.Collections.Generic;
class Program {
static int MinGroups( string s)
{
Stack< char > st = new Stack< char >();
int n = s.Length;
for ( int i = 0; i < n; i++) {
if (st.Count == 0 || s[i] != st.Peek()) {
st.Push(s[i]);
}
else {
st.Pop();
st.Push(s[i]);
}
}
return st.Count;
}
static void Main()
{
string s = "TTWWW" ;
Console.WriteLine(MinGroups(s));
}
}
|
Javascript
function minGroups(s) {
const stack = [];
const n = s.length;
for (let i = 0; i < n; i++) {
if (stack.length === 0 || s[i] !== stack[stack.length - 1]) {
stack.push(s[i]);
} else {
stack.pop();
stack.push(s[i]);
}
}
return stack.length;
}
const s = "TTWWW" ;
console.log(minGroups(s));
|
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(n), since we are using stack.
Share your thoughts in the comments
Please Login to comment...