package leetcode;
/**
* Created by w84108989 on 2019/1/24.
*/
//给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1。
//
// 举个例子,A = "abcd",B = "cdabcdab"。
//
// 答案为 3, 因为 A 重复叠加三遍后为 “abcdabcdabcd”,此时 B 是其子串;A 重复叠加两遍后为"abcdabcd",B 并不是其子串。
public class pipei_686 {
public static void main(String[] args) {
//
// repeatedStringMatch("abcd","cdabcdab");
System.out.println(repeatedStringMatch("abc","cabcabca"));
}
public static int repeatedStringMatch(String A, String B) {
char begin = B.charAt(0);
String A1 = "";
int j = A.indexOf(begin);
if (j == -1 ){
return -1;
}
for (int i = 0; i < B.length(); i++) {
int m = A.indexOf(B.charAt(i));
if (m == -1) {
return -1;
}
}
for (int i = 1; i < B.length() / A.length() + 100 ; i++) {
A1 = A + A1;
if (A1.indexOf(B) != -1){
return i;
}
// while(j < A1.length()){
// int index = A1.indexOf(begin,j);
// if (index == -1){
// return -1;
// }
// if (A1.length() - index >= B.length()){
// if (A1.substring(index,B.length()+index).equals(B)){
// System.out.println(A1.substring(index,B.length()+index));
// return i;
// }else {
// j += 1;
// }
// }
// }
}
return -1;
}
}