LC 828. Unique Letter String

link

LC 828. Unique Letter String

 

 

class Solution {
public:
    #define ll long long
    int mod=1E9+7;
    int uniqueLetterString(string S) {
        int n=S.size();
        ll res=0;
        vector<int> last1(26,-1);
        vector<int> last2(26,-1);
        for(int i=0;i<n;++i){
            char c=S[i];
            res+=(last2[c-'A']-last1[c-'A'])*(i-last2[c-'A'])%mod;
            res%=mod;
            last1[c-'A']=last2[c-'A'];
            last2[c-'A']=i;
        }
        for(int i=0;i<26;++i){
            if(last2[i]!=-1){
                res+=(last2[i]-last1[i])*(n-last2[i])%mod;
                res%=mod;
            }
        }
        return res;
    }
};

 

上一篇:智能指针之独占指针


下一篇:python 全栈开发,Day125(HTML5+ 初识,HBuilder,夜神模拟器,Webview)