Given a string S consisting of ‘X’, ‘Y’ and ‘Z’ only, the task is to convert S to a string consisting of only a single distinct character by selecting a character and removing substrings that does not contain that character, minimum number of times.
Note: Once a character is chosen, no other character can be used in further operations.
Examples:
Input: S = “XXX”
Output: 0
Explanation: Since the given string already consists of a single distinct character, i.e. X, no removal is required. Therefore, the required count is 0.Input: S = “XYZXYZX”
Output: 2
Explanation:
Selecting the character ‘X’ and removing the substrings “YZ” in two consecutive operations reduces the string to “XXX”, which consists of a single distinct character only.
Approach: The idea is to count occurrences of each character using unordered_map and count the number of removals required for each of them and print the minimum. Follow the steps below to solve the problem:
- Initialize an unordered_map and store the indices of occurrences for each of the characters.
- Iterate over all the characters of the string S and update occurrences of the characters ‘X’, ‘Y’ and ‘Z’ in the map.
- Iterate over the Map and for each character, count the number of removals required for each character.
- After calculating for each character, print the minimum count obtained for any of the characters.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find minimum removals // required to convert given string // to single distinct characters only int minimumOperations(string s, int n) { // Unordered map to store positions // of characters X, Y and Z unordered_map<char, vector<int> > mp; // Update indices of X, Y, Z; for (int i = 0; i < n; i++) { mp[s[i]].push_back(i); } // Stores the count of // minimum removals int ans = INT_MAX; // Traverse the Map for (auto x : mp) { int curr = 0; int prev = 0; bool first = true; // Count the number of removals // required for current character for (int index : (x.second)) { if (first) { if (index > 0) { curr++; } prev = index; first = false; } else { if (index != prev + 1) { curr++; } prev = index; } } if (prev != n - 1) { curr++; } // Update the answer ans = min(ans, curr); } // Print the answer cout << ans; } // Driver Code int main() { // Given string string s = "YYXYZYXYZXY"; // Size of string int N = s.length(); // Function call minimumOperations(s, N); return 0; } |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
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.
Recommended Posts:
- Minimum changes required to make first string substring of second string
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Reduce string by removing outermost parenthesis from each primitive substring
- Minimum removals from array to make GCD greater
- Maximum number of removals of given subsequence from a string
- Count minimum character replacements required such that given string satisfies the given conditions
- Minimum number of given operations required to reduce the array to 0 element
- Reduce a given Binary Array to a single element by removal of Triplets
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum substring flips required to convert given binary string to another
- Count number of Distinct Substring in a String
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Maximized partitions of a string such that each character of the string appears in one substring
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Partition array into minimum number of equal length subsets consisting of a single distinct value
- Minimum length of substring whose rotation generates a palindromic substring
- Count pairs whose product contains single distinct prime factor
- Minimum removal of subsequences of distinct consecutive characters required to empty a given string
- Find if a given string can be represented from a substring by iterating the substring “n” times
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.

