classSolution{public:intdistinctSubseqII(strings){constexprintkMod=1'000'000'007;// endsIn[i] := the number of subsequence that end in ('a' + i)vector<long>endsIn(26);for(constcharc:s)endsIn[c-'a']=accumulate(endsIn.begin(),endsIn.end(),1L)%kMod;returnaccumulate(endsIn.begin(),endsIn.end(),0L)%kMod;}};
1 2 3 4 5 6 7 8 9101112
classSolution{publicintdistinctSubseqII(Strings){finalintMOD=1_000_000_007;// endsIn[i] := the number of subsequence that end in ('a' + i)long[]endsIn=newlong[26];for(finalcharc:s.toCharArray())endsIn[c-'a']=(Arrays.stream(endsIn).sum()+1)%MOD;return(int)(Arrays.stream(endsIn).sum()%MOD);}}
1 2 3 4 5 6 7 8 910
classSolution:defdistinctSubseqII(self,s:str)->int:MOD=1_000_000_007# endsIn[i] := the number of subsequence that end in ('a' + i)endsIn=[0]*26forcins:endsIn[ord(c)-ord('a')]=(sum(endsIn)+1)%MODreturnsum(endsIn)%MOD