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
Your use of LinkedList is interesting (and potentially unexpected), but some of the other aspects are a bit strange & distracting ...
My first instinct would have been to repeatedly loop over the String, replacing characters into a StringBuilder
- with a while loop surrounding the foor loop (as suggested by sehe). This may be what the interviewer was expecting, rather than your clever use of LinkedList.
The interviewer may be distracted by these other aspects. e.g.:
LinkedList
instead of LinkedList
.LinkedList
directly from deconstruct(String)
. No need to wrap it.reconstruct()
method. Just use temp.size()
Regex is a bit of an undesirable way to get the third char. I can't think of a one-liner, but you could use an array, like so:
private static Character getThirdChar(Character firstChar, Character secondChar) {
List li= new ArrayList(Arrays.asList('a', 'b', 'c'));
li.remove(firstChar);
li.remove(secondChar);
return li.get(0);
}
After these edits, the interviewer might be able to focus more clearly on your very interesting solution.
EDIT: perhaps the question is asking you to return the smallest string itself, not its length. I think last line of the interview question ought to read as follows: "For the given string, return the smallest string which can result by applying this operation repeatedly"
HTH