# leetcode题解-686. Repeated String Match && 38. Count and Say

Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.

For example, with A = "abcd" and B = "cdabcdab".

Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").

Note:
The length of A and B will be between 1 and 10000.

    public int repeatedStringMatch(String A, String B) {
StringBuilder res = new StringBuilder(A);
int count = 1;
while(res.indexOf(B) < 0){
//不断添加A，直到res的长度大于B，这时如果res包含B，则返回添加次数，否则说明无法包含，返回-1。
//这里使用res的长度大于A+B的长度，是因为如果一开始A就比B大的话，不进行这一步判断会出错。比如A="abababaaba"，B="aabaaba"
if(res.length() - A.length() > B.length()) return -1;
res.append(A);
count ++;
}
return count;
}

    //88%
public int repeatedStringMatch2(String A, String B) {
if(A.length() > B.length()){
if(A.contains(B))
return 1;
else if((A+A).contains(B))
return 2;
}
StringBuilder res = new StringBuilder(A);
int count = 1;
while(res.indexOf(B) < 0){
if(res.length() > B.length()) return -1;
res.append(A);
count ++;
}
return count;
}


    //99%
public int repeatedStringMatch1(String A, String B) {
int count = 1;
int i = 0;
for (int j = 0; j < B.length(); j++) {
if (A.charAt(i) != B.charAt(j)) {
if (count > 1) {       // already second time: no way to make B from A
return -1;
}
j = -1;    // try to match j's starting character with next i
}

i++;
if (i == A.length()) {        // one more time of A
if (j == B.length() - 1) {
break;
}
count++;
i = 0;
}
}
return count;
}

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"
Example 2:

Input: 4
Output: "1211"


    //65%
public String countAndSay(int n) {
StringBuilder curr=new StringBuilder("1");
StringBuilder prev;
int count;
char say;
for (int i=1;i<n;i++){
prev=curr;
curr=new StringBuilder();
//分别用于统计和记录数字的个数和数字
count=1;
say=prev.charAt(0);

for (int j=1,len=prev.length();j<len;j++){
if (prev.charAt(j)!=say){
curr.append(count).append(say);
count=1;
say=prev.charAt(j);
}
else count++;
}
curr.append(count).append(say);
}
return curr.toString();

}

#### Leetcode-686. Repeated String Match(string)

2017-10-03 09:30:57

#### Repeated String Match问题及解法

2017-10-13 12:18:49

#### LeetCode Weekly Contest 52 Repeated String Match（string）

2017-10-01 11:46:31

#### LeetCode 38 Count and Say(C,C++,Java,Python)

2015-05-19 21:36:24

#### leetCode 38.Count and Say (计数和发言) 解题思路和方法

2015-07-09 20:40:16

#### LeetCode --- 38. Count and Say

2015-02-05 23:33:41

#### LeetCode 38 : Count and Say (Java)

2015-12-02 22:50:52

#### LeetCode 38 Count and Say（计数与报数）

2015-12-03 10:32:47

#### LeetCode38 Count and Say(C语言解决)

2016-11-15 12:13:21

#### [leetcode]38. Count and Say(java)

2017-01-09 14:19:02