Simple Python3 solution O(N) time, O(1) space using defaultdict
Adapted from https://leetcode.com/its_dark/ solution: https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/129021/O
Adapted from [https://leetcode.com/its_dark/](its_dark) solution: https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/129021/O(N)-Java-Solution-DP-Clear-and-easy-to-Understand/265186\n\n```\nfrom collections import defaultdict\nclass Solution:\n    def uniqueLetterString(self, s: str) -> int:\n        """\n        The key ideas behind the solution:\n        1. The maximum possible substrings that can end at an index are i + 1.\n        2. The contribution of a character to this substring is (i + 1) - it\'s last seen position.\n        3. At each point, sum of all contributions, gives the number of total substrings found so far.\n        4. The last seen position of char is actually i + 1.\n        """\n        last_position = defaultdict(int) # Used for storing the last position of each character\n        contribution = defaultdict(int) # Used for storing the contribution of each character so far. This\n        # will possibly be updated throughout the string traversal\n        res = 0\n        \n        for i, char in enumerate(s):\n            max_possible_substrs_at_idx = i + 1\n            contribution[char] = max_possible_substrs_at_idx - last_position[char]\n            \n            res+=sum(contribution.values())\n            last_position[char] = i + 1\n            \n        return res\n```
['Python', 'Python3']