A simple solution c++ O(N) runtime complexity
\nFor each character index in string if can know the prev and next occurence of indexes of the same character. Then with respect to current index character we c
**\nFor each character index in string if can know the prev and next occurence of indexes of the same character. Then with respect to current index character we can choose any index towards left till prev i.e in range[prev,curr_index] similary towards right we can choose any index in range [curr_index,right]\nSo all the unique substring for our current index is given by (curr_index-prev) * (right-curr_index)\n**\n\n```\nclass Solution {\npublic:\n    int uniqueLetterString(string s) {\n        vector<vector<int>>v(26);\n        int ans=0,n=s.length(),m,prev_ind,next_ind,left,right;\n        for(int i=0;i<n;i++){\n            v[s[i]-\'A\'].push_back(i);\n        }\n        for(int i=0;i<26;i++){\n            m=v[i].size();\n            for(int j=0;j<m;j++){\n                prev_ind=j-1;\n                next_ind=j+1;\n                if(prev_ind>=0){\n                    left=v[i][j]-v[i][prev_ind];\n                }\n                else{\n                    left=v[i][j]+1;\n                }\n                if(next_ind<m){\n                    right=v[i][next_ind]-v[i][j];\n                }\n                else{\n                    right=n-v[i][j];\n                }\n                ans+=(left*right);\n            }\n        }\n        return ans;\n    }\n};\n```
[]