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){finalintkMod=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)%kMod;return(int)(Arrays.stream(endsIn).sum()%kMod);}}
1 2 3 4 5 6 7 8 910
classSolution:defdistinctSubseqII(self,s:str)->int:kMod=1_000_000_007# endsIn[i] := the number of subsequence that end in ('a' + i)endsIn=[0]*26forcins:endsIn[string.ascii_lowercase.index(c)]=(sum(endsIn)+1)%kModreturnsum(endsIn)%kMod