当前位置 : 主页 > 编程语言 > java >

686. Repeated String Match

来源:互联网 收集:自由互联 发布时间:2022-08-10
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 thr


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.

class Solution {
public int repeatedStringMatch(String A, String B) {
String t = A ;
for(int i = 1 ; i <= B.length()/A.length()+2 ; i++){
if (t.indexOf(B) != -1)
return i ;
else
t += A


上一篇:网络编程之简介
下一篇:没有了
网友评论