python solution | detail explained and easy to understand
we may need read the problem carefully, if we clearly understand what is this asking for, it will be easy to find out a solution... \nso, "returns the number of
we may need read the problem carefully, if we clearly understand what is this asking for, it will be easy to find out a solution... \nso, "returns the number of unique characters on s", what does this mean? \ne.g.\n```\nIncorrect undersanding:\nSTRING = AAB\nsub-strings\nA -> 1\nA -> 0 already count A, hence 0 here\nB -> 1\nAA -> 1 beucze AA only has 1 unique characotrs A\nAB -> 2\nAAB -> 2 only has A,B tow unique chars\nso, total is 1 + 1 + 1 + 2 + 2 = 7\nbut his this not correct... \n\nSee this correct understanding of this problem:\nAAB \nA -> 1, only 1 unique charactors in this substring, A,\nA -> 1, only 1 unique charactors in this substring, A,\nB -> 1, like above\nAA -> 0, becuze A appeared twice, there\'s no unique charactors here.\nAB -> 2, one A and one B.\nAAB -> 1, AA is not unnique, B is unique here.. \nso, total is 1 + 1 + 1 + 0 + 2 + 1 = 6 \n```\nokay, we are looking for **how many charactors appeared just once**  in a substring, like `AAB`, there\'s only one unique charactor `B` apperased once, not **how many unique charactors there** `A` and `B`.\nso, the point of asking here is, \nas a charactors `A`, how many substirng i can be unique in this sub-string...\ne.g.\nstring: M N O P A B B A C D E\n\'A\' as unqie in below 2 sub srings:\nsub-str1: M N O P A B B \nsub-str2: B B A C D E \nuse first substring as an sample:\n1st part: M N O P A -> `A` can be MNOPA, NOPA, OPA, PA, A -> 5 times\n2nd part: B B -> `A` we have 2 extra charactors\nCOMBIN: \nMNOPAB NOPAB OPAB, PAB, AB\nMNOPABB MOPABB OPABB, PABB, ABB \nPlust the oringial ones, then we got the total as 15 times\nso here, easily we find out, for any charactor first, like `A`, and then expand to its left and right, aading those possiblities... \nto above cases. 5 + 5 + 5, or we would see, 5 * 3, \n1st part, we got the 5 possiblities, 2nd part, we have extra 2 charactors for `B`, `BB`, we can always add these 2nd part\'s subsring to the original 5 substrings to be new ones, which is 5 * (2 + 1), 1 here is the orignial 5 substrings. \nso, (4 -(-1)) * (7-4), 4 the 1st A index, 7 the 2nd A index, -1 is the boundary. \nTips:\nwhat is the left or right boundaries? \nIt could be the index of `prev-A` and `next-A`, or the edges of the oringal string 0 and n-1.\nas we are 0 indexed, we can use -1 for 0, n for n-1 \nthe above cases: \nthe first charactors `A` and the last charactors `A` bonundaries:\n`A` index[4, 7] could be change to [-1, 4, 7, 11], \n4-(-1) * (7-4) = 15\n(7-4) * (11-7) = 12\n15 + 12 = 27\n\n```\nclass Solution:\n    def uniqueLetterString(self, s: str) -> int:\n        # find all the characotrs\' indexes first\n        char_idx = defaultdict(list)\n        for i, char in enumerate(s):\n            char_idx[char].append(i)\n        \n        res = 0\n        for char in char_idx:\n            all_idxs = [-1] + char_idx[char] + [len(s)] # adding boundaries for each charactors\n            for i in range(1, len(all_idxs)-1):\n                res += (all_idxs[i] - all_idxs[i-1]) *  (all_idxs[i+1] - all_idxs[i])\n        \n        return res\n\n
['Python', 'Python3']