Given a string of any combination of three letters ‘a’, ‘b’, and ‘c’, find length of the smallest string that can be obtained by applying the following operation repeatedly:
Take any two adjacent, distinct characters and replace them with the third.
Input : cab Output : 2 We can select any two adjacent letters, say 'ca' and transform it into 'b', this leaves us with string 'bb' of length two. Input : bcab Output : 1 Selecting 'bc' and transforming it to 'a' leaves us with 'aab'. We can then select 'ab' and transform it to 'c', giving 'ac'. This can further be transformed into 'b', which is of length one.
A naive way to do this would be to find all possible replacements, and recurse until we find the minimum string. This would take exponential time.
Lemma: Order of letters does not effect the length or value of minimum string.
Proof By Induction
Base case: Take string ‘ab’ and ‘ba’, they both reduce to ‘c’
Inductive Hypothesis: All strings of length <= k reduce to the same string assuming the number of occurrences of each letter in each string is the same.
Inductive Step: Take two strings of length k + 1 having same number of occurrences of each letter. Find a pair of letters that are adjacent
in both strings. Here, two cases arise:
- We manage to find such a pair of letters. We can then replace these letters with the third letter, thus getting two strings of length k having same occurrences of each letter, which by inductive hypothesis reduces to the same string. i.e. We have ‘abcacb’ and ‘accbba’ and reduce ‘ac’ in both strings, we thus get ‘abcbb’ and ‘bcbba’.
- We cannot find such a pair. This arises when all letters in the string are the same. In this case, the two strings themselves are the same i.e. ‘ccccccc’ and ‘ccccccc’.
Thus by induction we have proven this lemma.
Dynamic Programming Approach
We can now devise a function using Dynamic Programming to solve this problem.
In the worst case, each letter is present in 1/3rd of the whole string. This leads to auxiliary space = O(N3) and time complexity = O(N3)
Space Complexity = O(N^3) Time Complexity = O(N^3)
We can do better than this using three main principles:
- If the string cannot be reduced further, then all letters in the string are the same.
- The length of minimum string is either <= 2 or equal to the length of original string, or 2 < minimum string length < original string length is never true.
- If each letter of the string is present an odd amount of times, after one reduction step, they shall all be present an even amount of times. The converse is also true, that is, if each letter of the string is present an even amount of times, they shall be present an odd amount of times after one reduction step.
These can be proven as follows:
- If any two different letters are present, we can select these and reduce string length further.
- Proof by contradiction:
Assume we have a reduced string of length less than original string. For example 'bbbbbbb'. Then this string must have originated from a string like 'acbbbbbb', 'bbacbbbb' or any other such combination of the same. In this case, we could have selected 'bc' instead of 'ac' and reduced further.
- From the recursive step above, we increase one letter by one and decrease the other two by one. So if we had a combination as (odd, odd, odd), then it would become (odd + 1, odd – 1, odd – 1) or (even, even, even). The reverse is shown in a similar fashion.
Now we can combine the above principles.
If the string consists of the same letter repeating, it's minimum reduced string is itself, and length is the length of the string.
Now, the other possible options are reduced string being of one character length or two. Now if all characters are present an even number of times, or an odd number of times, the only answer that is possible is 2, because (0, 2, 0) is (even, even, even) while (0, 1, 0) is (even, odd, even) so only 2 preserves this evenness.
In any other condition, the answer becomes 1.
Time Complexity: O(n)
Auxiliary Space: O(1)
This article is contributed by Aditya Kamath. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Length of the smallest sub-string consisting of maximum distinct characters
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
- Lexicographically smallest string of length N and sum K
- Smallest Palindrome after replacement
- Smallest Greater (than S) String of length K whose letters are subset of S
- String with k distinct characters and no same characters adjacent
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Minimum string such that every adjacent character of given string is still adjacent
- Repeated subsequence of length 2 or more
- Number formed after K times repeated addition of smallest divisor of N
- Character replacement after removing duplicates from a string
- Number of subsequences of maximum length K containing no repeated elements
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Find distinct characters in distinct substrings of a string
- Check if any permutation of string is a K times repeated string
- Longest sub string of 0's in a binary string which is repeated K times
- Minimize the cost to make all the adjacent elements distinct in an Array
- Number of ways to form an array with distinct adjacent elements
- Check if a string can be repeated to make another string