Created
January 22, 2019 19:55
-
-
Save kylelong/5a8c711471f7cd6f956342d146f5165b to your computer and use it in GitHub Desktop.
Week 1/20/19 of Cassido's newsletter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
repeat("foo", "1foo2foo3foo4")
is 3 not -1