Tavant interview question

DS problem. Frequency count, bracket balance.