Who can be easier to understand?
dp[i] = how many unique chars for all substrings ended with i. Then we should return sum(dp[i]).\neg: suppose for "ABCDQEFGHIQJKLMNOPQ", we know dp[17] at "P" a
dp[i] = how many unique chars for all substrings ended with i. Then we should return sum(dp[i]).\neg: suppose for "ABCDQEFGHIQJKLMNOPQ", we know dp[17] at "P" already, we want to know dp[18] at "Q".\n1. We know the first_q_idx = 4, second_q_idx = 10, curr_q_idx = 18.  \n2. There are 3 sections in "ABCDQEFGHIQJKLMNOPQ". Section 1: "ABCDQ", Section 2: "EFGHIQ" and Section 3: "JKLMNOPQ"\n3. For section 3, eg: one substring ended with i = 18 is "MNOPQ". \n\t We know countUniqueChars("MNOPQ") = countUniqueChars("MNOP") + 1, the same goes for other valid substrings using section 3.\n\t So now we have dp[18] = dp[17] + (curr_q_idx - second_q_idx)\n4. For section 2, eg: one substring ended with i = 18 is "HIQJKLMNOPQ".\n    We know countUniqueChars("HIQJKLMNOPQ") = countUniqueChars("HIQJKLMNOP") - 1, the same goes for other valid substrings using section 2.\n\tSo now we have dp[18] = dp[17] + (curr_q_idx - second_q_idx) - (second_q_idx - first_q_idx)\n5. For section 1, eg: one substrind ended with i = 18 is "CDQEFGHIQJKLMNOPQ".\n    We know countUniqueChars("CDQEFGHIQJKLMNOPQ") = countUniqueChars("CDQEFGHIQJKLMNOP"), the same goes for other valid substrins using section 1.\n\tSo now we have dp[18] = dp[17] + (curr_q_idx - second_q_idx) - (second_q_idx - first_q_idx)\n\n```\nclass Solution:\n    def uniqueLetterString(self, s: str) -> int:\n        dp = [0 for _ in range(len(s))]     # dp[i] = how many unique chars for all substrings ended with i\n        dp[0] = 1\n        \n        idxes = [(-1, -1) for _ in range(26)]   # Keep track of (first_idx, second_idx) for every char.\n        idxes[ord(s[0]) - ord(\'A\')] = (-1, 0)\n        \n        for i in range(1, len(s)):\n            first_idx, second_idx = idxes[ord(s[i]) - ord(\'A\')]     # Obtain the first_idx and second_idx of s[i]\n            dp[i] = dp[i - 1] + (i - second_idx) - (second_idx - first_idx)\n            \n            idxes[ord(s[i]) - ord("A")] = (second_idx, i)           # update the first_idx and second_idx of s[i]\n            \n        return sum(dp) % (10 ** 9 + 7)\n```
['Python']