Define s = [S,n] as the string s which consists of n connected strings S. For example, ["ABC", 3] = "ABCABCABC" . On the other hand, we define that string S1 can is obtained from string S2 if We can remove some characters from S2 such that it becomes S1. For example, "ABC" Can is obtained from "Abdbec" based in our definition, but it can is not the obtained from "Acbbe". You is given non -empty strings S1 and S2 (each at the most characters long ) and integers 0≤n1≤106 and 1≤n2≤106. Now consider the strings S1 and S2, where s1=[s1,n1] and S2=[s2,n2]. Find The maximum integer M such that [s2,m] can is obtained from S1. EXAMPLE:INPUT:S1 = "ACB", N1=4s2 = "AB", n2=2 return: 2
Currently only came up with the brute force approach (1165MS), see <20ms practices, did not delve into:
1 Public classSolution {2 Public intGetmaxrepetitions (String s1,intN1, String S2,intn2) {3 Char[] arr1 =S1.tochararray ();4 Char[] arr2 =S2.tochararray ();5 intI1 = 0, i2 = 0;//Current position6 intCount1 = 0, count2 = 0;//# times pass through S1/s2 ' s end7 8 while(Count1 <N1) {9 if(Arr1[i1] = =Arr2[i2]) {Teni2++; One if(I2 = =arr2.length) { Ai2 = 0; -count2++; - } the } -i1++; - if(I1 = =arr1.length) { -I1 = 0; +count1++; - } + } A returncount2/N2; at } -}
Leetcode:count the repetitions