Taken from DailyCodingProblem
You are given a string consisting of the letters
x
andy
, such asxyxxxyxyy
. In addition, you have an operation calledflip
, which changes a singlex
toy
or vice versa.Determine how many times you would need to apply this operation to ensure that all
x
’s come before ally
’s. In the preceding example, it suffices to flip the second and sixth characters, so you should return2
.
Solution
This is the answer I came up with using JavaScript:
And also implemented using Ruby: