Given a string of ‘0’, ‘1’ and ‘2’. The task is to find the minimum number of replacements such that the adjacent characters are not equal.
Input: s = “201220211”
Resultant string after changes is 201210210
Input: s = “0120102”
Approach: The following problem can be solved using greedy method. We can greedily compare every adjacent pair. If the adjacent pairs which is character at ith and i-1th are same, then replace ith th character with a character which is not equal to the character at i-1th and i+1th index. In case of the last adjacent pair, just replace it with the character which is not equal to the character at i-1th index.
Below is the implementation of the above approach:
Time Complexity : O(n)
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Minimum replacements in a string to make adjacent characters unequal
- Minimize replacements by previous or next alphabet required to make all characters of a string the same
- Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Convert string X to an anagram of string Y with minimum replacements
- Minimum string such that every adjacent character of given string is still adjacent
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Minimum replacements required to obtain a K-periodic palindromic string
- Count minimum character replacements required such that given string satisfies the given conditions
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Minimum characters to be replaced to make frequency of all characters same
- Maximum and minimum sums from two numbers with digit replacements
- Min flips of continuous characters to make all characters same in a string
- Minimum characters to be replaced to make a string concatenation of a K-length palindromic string
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.