''' > https://www.codingame.com/ide/puzzle/xml-mdf-2016 @author: olivier.massot, 2019 ''' s = "ab-bcd-d-c-ae-e" opened, depth, score = True, 0, {} for c in s: if c == '-': opened = False elif opened: depth += 1 else: score[c] = score.get(c, 0) + 1 / depth depth -= 1 opened = True print(score) print(max(score, key=score.get))