其他分享
首页 > 其他分享> > LC 828. Unique Letter String

LC 828. Unique Letter String

作者:互联网

link

 

 

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;
    }
};

 

标签:26,String,int,ll,long,vector,last1,Unique,LC
来源: https://www.cnblogs.com/FEIIEF/p/12342724.html