From Coderbyte:
Have the function StringReduction(str) take the str parameter being passed and return the smallest number you can get through the following reduction method. The method is: Only the letters a, b, and c will be given in str and you must take two different adjacent characters and replace it with the third. For example "ac" can be replaced with "b" but "aa" cannot be replaced with anything. This method is done repeatedly until the string cannot be further reduced, and the length of the resulting string is to be outputted. For example: if str is "cab", "ca" can be reduced to "b" and you get "bb" (you can also reduce it to "cc"). The reduction is done so the output should be 2. If str is "bcab", "bc" reduces to "a", so you have "aab", then "ab" reduces to "c", and the final string "ac" is reduced to "b" so the output should be 1.
Source ( passed all validations ):
var replacements = { "ab" : "c", "ac" : "b", "bc" : "a", "ba" : "c", "ca" : "b", "cb" : "a" }; function StringReduction(s) { var original, key; while( s != original ) { original = s; for( key in replacements ) s = s.replace( key , replacements[key] ); } return s.length; } // keep this function call here // to see how to enter arguments in JavaScript scroll down StringReduction(readline());
The funny part of this challenge is that you write against time, so the first version was a terrible, recursive approach. Since then I rewrote it a few times before posting to CR, thinking each time mistakenly that it was perfect, at this point I would like to get your feedback on it.