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 problem has been solved using a greedy approach in the previous post. In this post, we will discuss a Dynamic Programming approach to solve the same problem. Create a function charVal which returns 0, 1 or 2 depending on the character.
A recursive function is made which calls the charVal function to get the i-th value, and if this is equal to the previous one, then only the other two states (1 or 2, 0 or 1, 0 or 2) is used at i-th character. If it is not equal to the previous one, no changes are made. A dp array is used for memoization. The base case is when all the positions are filled. If any of the state is re-visited, then return the value that is stored in dp array. DP[i][j] means that i-th position is filled with j-th character.
Below is the implementation of the above problem:
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.
- Minimum replacements to make adjacent characters unequal in a ternary string
- Minimum replacements in a string to make adjacent characters unequal
- 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
- 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
- Min flips of continuous characters to make all characters same in a string
- Maximum and minimum sums from two numbers with digit replacements
- Minimize the number of replacements to get a string with same number of 'a', 'b' and 'c' in it
- Count numbers in range such that digits in it and it's product with q are unequal
- XOR two binary strings of unequal lengths
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
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.