# 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)

• zsheng_
• 2017年10月03日 09:30
• 830

## leetcode_686.Repeated String Match ? 待解决

Repeated String MatchGiven two strings A and B, find the minimum number of times A has to be repeate...
• wjheha
• 2017年10月23日 16:10
• 96

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

• huatian5
• 2017年10月01日 11:46
• 364

## LeetCode --- 38. Count and Say

• makuiyu
• 2015年02月05日 23:33
• 2384

## LeetCode 38 : Count and Say (Java)

• changetocs
• 2015年12月02日 22:50
• 1107

## LWC 52：686. Repeated String Match

LWC 52：686. Repeated String Match传送门：686. Repeated String MatchProblem: Given two strings A and B,...
• u014688145
• 2017年10月02日 15:11
• 1160

## [LeetCode] 038. Count and Say (Easy) (C++/Python)

[LeetCode] 038. Count and Say (Easy) (C++/Python)
• hcbbt
• 2015年07月30日 10:04
• 1823

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

Problem: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 2...
• runningtortoises
• 2015年05月19日 21:36
• 2199

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

• NoMasp
• 2015年12月03日 10:32
• 4244

## leetcode 38 Count and Say C++

• a2331046
• 2016年06月08日 10:28
• 350

举报原因： 您举报文章：leetcode题解-686. Repeated String Match && 38. Count and Say 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)