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++ implementation to Count the // minimum number of groups formed in a string // by replacing consecutive characters // with same single character #include<bits/stdc++.h> 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 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 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# 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 |
<script> // Javascript 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 function group_formed(S)
{ // Initializing count as one since
// the string is not NULL
let count = 1;
for (let i = 0; i < S.length - 1; i++)
{
// Comparing adjacent characters
if (S[i] != S[i + 1])
count += 1;
}
document.write(count);
} // Driver code let S = "TTWWW" ;
group_formed(S); // This code is contributed by divyesh072019 </script> |
2
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:
#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;
} |
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();
}
// Driver code
public static void main(String[] args) {
String s = "TTWWW" ;
System.out.println(minGroups(s));
}
} |
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()
|
using System;
using System.Collections.Generic;
class Program {
// Function to calculate the minimum number of groups
static int MinGroups( string s)
{
Stack< char > st = new Stack< char >();
int n = s.Length;
for ( int i = 0; i < n; i++) {
// If the stack is empty or the current
// character is different from the top of the
// stack, push the current character onto the
// stack.
if (st.Count == 0 || s[i] != st.Peek()) {
st.Push(s[i]);
}
// If the current character is the same as the
// top of the stack, pop the top character from
// the stack and then push the current
// character.
else {
st.Pop();
st.Push(s[i]);
}
}
return st.Count;
}
static void Main()
{
string s = "TTWWW" ;
// Call the MinGroups function and print the result
Console.WriteLine(MinGroups(s));
}
} |
// Function to find the minimum number of groups required function minGroups(s) {
// Create an empty stack to store characters
const stack = [];
const n = s.length;
// Iterate through the input string
for (let i = 0; i < n; i++) {
// If the stack is empty or the current character is different from the top of the stack
if (stack.length === 0 || s[i] !== stack[stack.length - 1]) {
// Push the current character onto the stack
stack.push(s[i]);
} else {
// If the current character is the same as the top of the stack, pop it (canceling out adjacent duplicates)
stack.pop();
// Push the current character onto the stack again (forming a new group)
stack.push(s[i]);
}
}
// The length of the stack represents the minimum number of groups required
return stack.length;
} // Driver code const s = "TTWWW" ;
console.log(minGroups(s)); |
2
Time Complexity: O(n), where n is the length of the input string.
Space Complexity: O(n), since we are using stack.