Python, detailed explanation
"res[i]" means the result of all substrings ended at index "i". Final result is sum of all "res[i]"\nres[i] will be composed by all substrings of res[i-1] appen
"res[i]" means the result of all substrings ended at index "i". Final result is sum of all "res[i]"\nres[i] will be composed by all substrings of res[i-1] appending "i". For each substring in res[i-1], when we append "i" to it:\n1. If there is no "i" in this substring, e.g., append "A" to "B", substring changes from "B" to "BA", the UNIQ value of this substring changes from 1 to 2, we add 1 to all this kind of substrings.\n2. If there is one "i" in this substring, e.g. append "A" to "AB", substring changes from "AB" to "ABA", the substring UNIQ value changes from 2 to 1. We minus 1 to this kind of substrings.\n3. If there is more than one "i" in this substring, e.g. append "A" to "ABAB", substring changes from "ABAB" to "ABABA", the substring UNIQ value doesn\'t change. We do nothing to this kind of substrings.\n\nSo for each new "i", we only care how many same char before it, and what are the indexes of these chars. Actually we only need to know 2 same chars before it. Because situations more than 2 chars can be handled the same as 2 chars.\n\n```\nclass Solution:\n    def uniqueLetterString(self, S: str) -> int:\n        res=[0]*(len(S)+1)\n        idxs=[[-1,-1]]*26\n        for i,c in enumerate(S):\n            code=ord(c)-ord(\'A\')\n            first,second=idxs[code]\n            res[i+1]=1+res[i]+(i-1-second)-(second-first)\n            idxs[code]=[second,i]\n        return sum(res)%(10**9+7)\n```
[]