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.
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.
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:
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.
- Count of times second string can be formed from the characters of first string
- Count of strings that can be formed from another string using each character at-most once
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Find the number of strings formed using distinct characters of a given string
- Minimum number of given operations required to convert a string to another string
- Count minimum swap to make string palindrome
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Sort groups of numbers and characters separately in a String
- Minimum number of sub-strings of a string such that all are power of 5
- Transform One String to Another using Minimum Number of Given Operation
- Check whether second string can be formed from characters of first string
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of additons to make the string balanced
- Find minimum number of steps to reach the end of String
- Minimum number of subsequences required to convert one string to another
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.