Python 10 line,DP,O(1) Space, O(n) Time, Intuitive One pass solution
recording second and first appear letter.\n defind dp[i] as ended with i index string.\nsuppose t is rightmost index that S[i] appears,m is second rightmost ind
recording second and first appear letter.\n defind dp[i] as ended with i index string.\nsuppose t is rightmost index that S[i] appears,m is second rightmost index S[i] appear(if exist)\nother unique letter no need to consider, only consider \nm...t...i-1 i ,for l=m+1...t, unique comparing dp[i-1] each sub decrease by 1*(t-m)\nif t not exist. set t=-1\nif m not exist.set  m=-1\nfor l=t+1...i, increase by 1*(i-t)\ndp[i]=dp[i-1]+i-t-(t-m)\nSo happy to solve mutiple hard problem all by myself recently,\u5624\u5624\u5624\n ```\nclass Solution(object):\n    def uniqueLetterString(self, s):\n        res=lastdp=0\n        letter=[[-1,-1] for _ in range(26)]#(rightmost,secondright)\n        for i in range(len(s)):\n            t,m=letter[ord(s[i])-ord(\'A\')]\n            lastdp+=i-2*t+m\n            res+=lastdp#current dp[i]\n            letter[ord(s[i])-ord(\'A\')]=[i,t]#update letter \n        return res%(10**9 + 7)\n        \n  \n
[]