Skip to content

Instantly share code, notes, and snippets.

@kylelong
Created January 22, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylelong/5a8c711471f7cd6f956342d146f5165b to your computer and use it in GitHub Desktop.
Save kylelong/5a8c711471f7cd6f956342d146f5165b to your computer and use it in GitHub Desktop.
Week 1/20/19 of Cassido's newsletter
import java.util.regex.*;
/**
* Created by kylel95 on 1/22/19.
*/
public class repeatedSubString {
public static void main(String [] args){
System.out.println(repeat("pi", "pipipipi"));
System.out.println(repeat("danger", "cassidy"));
}
/**
* Given two strings x and y, write a function that returns how many times x can be repeated to generate y.
* If y cannot be generated by repeating x, return -1.
* @param x needle, string that is possible in y
* @param y haystack, full string
* @return how many times x appears in y
*/
public static int repeat(String x, String y){
Pattern p = Pattern.compile(x);
Matcher m = p.matcher(y);
int count = 0;
if(m.find()){
count++;
while (m.find()) {
count++;
}
return count;
}
else{
return -1;
}
}
}
@Patterner
Copy link

repeat("foo", "1foo2foo3foo4") is 3 not -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment