structRecord{intcount;intnextIndex;};classSolution{public:intgetMaxRepetitions(strings1,intn1,strings2,intn2){// records[i].count := the number of times that s2 starting from index i has// been fully matched with s1// records[i].nextIndex := the next index in s2 to be matched after// completing a full match starting from index ivector<Record>records;for(inti=0;i<s2.length();++i){intcount=0;intnextIndex=i;for(constcharc:s1)if(s2[nextIndex]==c)if(++nextIndex==s2.length()){// There's a match.++count;nextIndex=0;}records.emplace_back(count,nextIndex);}intmatches=0;// the number of matches between `s1` x n1 and `s2`inti=0;// the index in `s2` to start matchingwhile(n1-->0){matches+=records[i].count;i=records[i].nextIndex;}returnmatches/n2;}};
classSolution{publicintgetMaxRepetitions(Strings1,intn1,Strings2,intn2){recordRecord(intcount,intnextIndex){}// records[i].count := the number of times that s2 starting from index i has// been fully matched with s1// records[i].nextIndex := the next index in s2 to be matched after// completing a full match starting from index iList<Record>records=newArrayList<>();for(inti=0;i<s2.length();++i){intcount=0;intnextIndex=i;for(finalcharc:s1.toCharArray())if(s2.charAt(nextIndex)==c)if(++nextIndex==s2.length()){// There's a match.++count;nextIndex=0;}records.add(newRecord(count,nextIndex));}intmatches=0;// the number of matches between `s1` x n1 and `s2`inti=0;// the index in `s2` to start matchingwhile(n1-->0){matches+=records.get(i).count;i=records.get(i).nextIndex;}returnmatches/n2;}}
fromdataclassesimportdataclass@dataclassclassRecord:count:intnextIndex:intclassSolution:defgetMaxRepetitions(self,s1:str,n1:int,s2:str,n2:int)->int:# records[i].count := the number of times that s2 starting from index i has# been fully matched with s1# records[i].nextIndex := the next index in s2 to be matched after# completing a full match starting from index irecords=[]fornextIndexinrange(len(s2)):count=0forcins1:ifs2[nextIndex]==c:nextIndex+=1ifnextIndex==len(s2):# There's a match.count+=1nextIndex=0records.append(Record(count,nextIndex))matches=0# the number of matches between `s1` x n1 and `s2`i=0# the index in `s2` to start matchingfor_inrange(n1):matches+=records[i].counti=records[i].nextIndexreturnmatches//n2
Thanks for stopping by! If you find this site helpful, consider buying me some protein powder to keep me fueled! 😄