A unique O(N) Time and O(1) Space solution | Different from all other approaches mentioned already
Real Problem Statement: We need to find the sum of count of unique characters of each substring.\n\nRephrased Problem statement: We can rephrase it as finding t
**Real Problem Statement:** We need to find the sum of count of unique characters of each substring.\n\n**Rephrased Problem statement:** We can rephrase it as finding the sum of count of substrings having frequency of **X** = 1 where \'A\' <= **X** <= \'Z\'.\n\nSince contribution of every character to final answer is equal to the number of subarrays in which it occurs, so this solution works correctly.\n\n```\nclass Solution {\npublic:\n    int getCount(string& str, char x) {\n        \n        int count = 0, n = str.size();\n        int prev = -1, prevCount = 0;\n        for(int i = 0; i < n; i++) {\n            if(str[i] == x) {\n                int newCount = i - prev;\n                count += prevCount * newCount;\n                prevCount = newCount;\n                prev = i;\n            }\n        }\n        int newCount = n - prev;\n        count += prevCount * newCount;\n        return count;\n    }\n    int uniqueLetterString(string str) {\n        int ans = 0;\n        for(char x = \'A\'; x <= \'Z\'; x++) {\n            ans += getCount(str, x);\n        }\n        return ans;\n    }\n};\n```\nDo upvote if you like the approach.
['String']