How can I achieve a repeated string match in programming?

2026-06-10 06:311阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计199个文字,预计阅读时间需要1分钟。

How can I achieve a repeated string match in programming?

给定两个字符串A和B,找出A需要重复的最小次数,使得B成为它的子串。如果不存在这样的解,则返回-1。例如,如果A=abcd和B=cdabcdab,则返回3,因为通过重复A三次,可以得到abcdabcdabcd,B是它的子串。


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


How can I achieve a repeated string match in programming?

本文共计199个文字,预计阅读时间需要1分钟。

How can I achieve a repeated string match in programming?

给定两个字符串A和B,找出A需要重复的最小次数,使得B成为它的子串。如果不存在这样的解,则返回-1。例如,如果A=abcd和B=cdabcdab,则返回3,因为通过重复A三次,可以得到abcdabcdabcd,B是它的子串。


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


How can I achieve a repeated string match in programming?