Leetcode:count the repetitions

Source: Internet
Author: User

 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

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.