• + 9 comments

    This is Python. The logic is simple - take all possible substrings, split them into two sets according to starting letter, then sum elements in sets.

    All possible substrings are string of lenght 1, then strings of length 2 etc. i above is an iterator for that lenght.

    And then comes a little optimization - if you know the starting letter, you can add all substrings of different length that start with this letter. It will be len(s) - i