This is an interview problem that I am stuck on:
Given a string consisting of a, b and c’s, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if ‘a’ and ‘c’ are adjacent, they can replaced with ‘b’. What is the smallest string which can result by applying this operation repeatedly?
My attempted solution:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
public class Solution {
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
System.out.println(solve(in.readLine()));
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static int solve(String testCase) {
LinkedList<String> temp = new LinkedList<String>(deconstruct(testCase));
for (int i = 0; i < (temp.size() - 1); i++) {
if (!temp.get(i).equals(temp.get(i + 1))) {
temp.add(i, getThirdChar(temp.remove(), temp.remove()));
i = -1;
}
}
return reconstruct(temp).length();
}
private static List<String> deconstruct(String testCase) {
List<String> temp = new LinkedList<String>();
for (int i = 0; i < testCase.length(); i++) {
temp.add(testCase.charAt(i) + "");
}
return temp;
}
private static String reconstruct(List<String> temp) {
String testCase = "";
for (int i = 0; i < temp.size(); i++) {
testCase += temp.get(i);
}
return testCase;
}
private static String getThirdChar(String firstChar, String secondChar) {
return "abc".replaceAll("[" + firstChar + secondChar + "]+", "");
}
}
The code seems to work fine on test inputs “cab” (prints “2”), “bcab” (prints “1”), and “ccccc” (prints “5”). But I keep getting told that my code is wrong. Can anyone help me figure out where the bug is?
As people have already pointed out the error is that your algorithm makes the substitutions in a predefined order. Your algorithm would make the transformation:
abcc --> cccinstead of
abcc --> aac --> ab --> cIf you want to use the technique of generating the reduced strings, you need to either:
If all you need is the length of the reduced string, there is however a much simpler
implementation which does not require the reduced strings to be generated. This is an extended version of @Matteo’s answer, with some more details and a working (very simplistic) algorithm.
Simple properties
I postulate that the following three properties are true about abc-strings under the given set of rules.
If it is impossible to reduce a string further, all the characters in that string must be the same character.
It is impossible that:
2 < answer < string.lengthis trueWhile performing a reduction operation, if the counts of each letter prior to the operation is even, the count of each letter after the operation will be odd. Conversely, if the counts of each letter is odd prior to the operation, the counts will be even after the operation.
Property 1
Property one is trivial.
Property 2 (example to illustrate)
Assume: we have a reduced string of length 5 which can be reduced no more.
AAAAAAs this string is the result of a reduction operation, the previous string must’ve contained one
Band oneC. Following are some examples of possible “parent strings”:BCAAAA,AABCAA,AAACBAFor all of the possible parent strings we can easily see that at least one of the C:s and the B:s can be combined with A:s instead of each other. This will result in a string of length 5 which will be further reducible. Hence, we have illustrated that the only reason for which we had an irreducible string of length 5 was that we had made incorrect choice of which characters to combine while performing the reduction operation.
This reasoning applies for all reduced strings of any length k such that
2 < k < string.length.Property 3 (example to illustrate)
If we have for example
[numA, numB, numC] = [even, even, even]and perform a reduction operation in which we substitute AB with a C. The count of A and B will decrease by one, making the counts odd, while the count of C will increase by one, making that count odd as well.Similarly to this, if two counts are even and one is odd, two counts will be odd and one even after the operation and vice versa.
In other words, if all three counts have the same “evenness”, no reduction operation can change that. And, if there are differences in the “evenness” of the counts, no reduction operation can change that.
Drawing conclusions which result from the properties
Consider the two irreducible strings:
AandAAFor
Anotice that[numA, numB, numC] = [odd, even, even]For
AAnotice that[numA, numB, numC] = [even, even, even]Now forget those two strings and assume we are given an input string of length n.
If all characters in the string are equal, the answer is obviously string.length.
Else, we know from property 2 that it is possible to reduce the string to a length smaller than 3. We also know the effect on evenness of performing reduction operations. If the input string contains even counts of all letters or odd count of all letters, it is impossible to reduce it to a single letter string, since it is impossible to change the evenness structure from
[even, even, even]to[odd, even, even]by performing reduction operation.Hence a simpler algorithm would be as follows:
Algorithm