Code
The solution of problem 984 is showed below. You can also find it here.
1 | class Solution { |
Solution
The core method of the problem is to make sure that the number of each character is no less than half of another character and no exceed two consecutively.
Valuable Method
I’ve tried to find pattern at first time, such as “aab”, “abb”, and etc. Finally, I find that if we want to prevent those “aaa” and “bbb” appear, we just need to keep the ratio of number of two characters. It’s easier to apply a simple rule rather than setting many patterns and it looks clean and beautiful.