"BANANA" only B is lone, while in "ANT" all three letters are.
Given a string word, count the lone letters of every non-empty substring and return the sum of those counts. Substrings with the same text at different positions are counted separately.
Checking every substring is too slow; aim for linear time.word="AB" → 4
The substrings are "A" (1), "B" (1) and "AB" (2), for a total of 4.
word="AAB" → 6
"A", "A" and "B" give 1 each, "AA" gives 0 (its letter repeats), "AB" gives 2 and "AAB" gives 1 (only B is alone). The total is 6.
word="ZZZ" → 3
word consists of uppercase English lettersword="AB"
4
Run checks the first 3 tests in your browser. Submit checks all 160 tests on the server, hidden ones included.