jeudi 19 février 2015

count number of non overlapping occurrences in a string

Hello i am trying to solve a problem where i count the number of non-overlapping occurrences of a string s2 in a string s1. Example String s1 = "abab" and String s2 = "ab" the number of occurrences is 2. Another example String s1 = "aaabbaa" and String s2 = "aa" the number of occurrences is 2. Final example if String s1 = "aabbaa" and String s2 = "AA" the number of occurrences equal 0. How would i go about the problem?


Idea: I know if i match the String s2 in s1. i move to the next position by adding the current index position with s2.length and if they are not equal increment the current index position by 1. My problem don't know how to start? How would you use iteration to solve this. Thanks for the help.


Aucun commentaire:

Enregistrer un commentaire