Given a string of ‘0’, ‘1’ and ‘2’. The task is to find the minimum replacements in the string such that the differences between the indexes of the same characters is divisible by 3.
Input: s = “2101200”
1201201 or 2102102 can be the resultant string
which has 3 replacements.
Input: s = “012”
Approach: There can be 6 different strings such that the difference between the index of similar characters is divisible by 3. Hence generate all 6 different strings, and compare the replacements done with the original string. Store the string which has a minimal number of replacements. The different strings can be generated using next_permutation in C++.
Below is the implementation of the above approach:
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum index i such that all the elements from index i to given index are equal
- Maximum and minimum sums from two numbers with digit replacements
- Minimum number of replacements to make the binary string alternating | Set 2
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Minimum number of operations to move all uppercase characters before all lower case characters
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Covert string X to an anagram of string Y with minimum replacements
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Program to find the maximum difference between the index of any two different numbers
- Count of strings where adjacent characters are of difference one
- Minimum Index Sum for Common Elements of Two Lists
- Count pairs of characters in a string whose ASCII value difference is K
- Minimum bit changes in Binary Circular array to reach a index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.