java easy solution in O(N)
\nsuppose you have AABABA\nTHEN STARTING FROM ZERO TO \nINDEX 0\nA-1\nINDEX 1\nAA-0\nA-1\nINDEX 2\nAAB-1\nAB-2\nB-1\nINDEX 3\nAABA-1\nABA-1\nBA-2\nA-1\nINDEX 4\
```\nsuppose you have AABABA\nTHEN STARTING FROM ZERO TO \nINDEX 0\nA-1\nINDEX 1\nAA-0\nA-1\nINDEX 2\nAAB-1\nAB-2\nB-1\nINDEX 3\nAABA-1\nABA-1\nBA-2\nA-1\nINDEX 4\nAABA-1\nABA-1\nBA-2\nA-1\nINDEX 5\nAABAB-0\nABAB-0\nBAB-1\nAB-2\nB-1\nINDEX 6\nAABABA-0\nABABA-0\nBABA-0\nABA-1\nBA-2\nA-1\n\nas you can see the index unique(substring till 6)=4 how see you have used some \'A\' before how many substring was formed using that a as last character of the substring it was at index 4 having 4 substring then what was \'A\' used before it  was index 2 with 2 subtring and first \'A\' at index \'1\' with 1 substring \nso starting with 1 how many characters 1\nthen at index 2 \nhow many character it should be at index 2 if it was not the same character as \'A\' it should be dp[i-1]+(i+1)=3 but because it was repeated it gave you have to -2 for AA so how new many \'A\' you used in minus part were 1  and unused is 1\nwhy this?\nsee A-1 first index\nat index 2 you mark \nAA-0 no unique character\nA-1 \n\nthen come to index 3\n1.AABA\n2.ABA\n3.BA\n4.A\nin this as you can observe one thing AABA you have used it in index 1 so there is no need to do -2 only -1 for new A \nthen comes ABA u have to do -2 because earlier in index 2 -\'B\'\nyou calculate for \'AB\'-2 so if u dont do at index 3 substring "ABA" -2 u will calculate the AB at index 2 +ABA at index 3 so u have to do -2 then how many used and how many a used u see the substring which contains "A" of index 2 and index 1 is substring 1 substring 2 of index 3. and unused is all leaving substring 1 and substring 2 . Doing this u will see that u got the result out here unused 2 and used 2 so i forget to tell what is unused and what is used for those substring which have  character u have calculate u put those in unused and the substring with fresh character u put in used \nfor example in substring(0,3) index 3 \nA at substring 1 and 2 are comming from previous and A at substring 3 and 4 is only A from index 3 and no A from other indexes \nused -2 \nunused 2\n\nso come to index 5\nu have dp[i-1]=4 dp[i-1]+6-2*used -1*unused=4+6-4-2=4 \nso i think this will definately help \n```\n**PLS DO UPVOTE IF U LIKE MY SOLUTION I M POSTING MY CODE TOO**\n\nfor refe\n\n\n\n\n```\nclass Solution \n{\n    public int uniqueLetterString(String s) \n    {\n        int n=s.length();\n        int used[]=new int[26];\n        int unused[]=new int[26];\n        int dp[]=new int[n+1];\n        used[(int)(s.charAt(0)-\'A\')]++;\n        dp[0]=1;\n        for(int i=1;i<n;i++)\n        {\n            char c=s.charAt(i);\n            int totalz_e=dp[i-1]+i+1;\n            dp[i]=totalz_e-2*used[c-\'A\']-1*unused[c-\'A\'];\n           \n            unused[c-\'A\']+=used[c-\'A\'];\n            used[c-\'A\']=i+1-unused[c-\'A\'];\n        }\n        int sum=0;\n        for(int i=0;i<n;i++)\n        {\n            sum+=dp[i];\n        }\n        System.out.println(Arrays.toString(dp));\n        return sum;\n    }\n}\n\n```\n\n\n\n\n\npls if u have any doubts regarding to my process pls ask me in doubt section\nTHANK YOU FOR YOUR SUPPORT
['Dynamic Programming', 'Java']