Solution to Beautiful String problem - Facebook Hacker Cup Qualification Round