题目地址:https://leetcode.com/problems/repeated-string-match/description/

题目描述

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.

Forexample, 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:

Thelength of A and B will be between 1 and 10000.

题目大意

判断当A重复一定次数之后,B能否成为其子串。

解题方法

自己想了很久没想出怎么做,可是看到别人的解法后,立马就懂了:当A重复一定次数后,长度比B长了,那么就可以停止了!因为如果这种情况下B都不是A的子串,那么循环再多也没用。。因为对于B来说,A所有可能的重复都已经出现了。

python的除会向下取整,所以多加几次,比如+3。(+2会错误)

class Solution(object):
    def repeatedStringMatch(self, A, B):
        """
        :type A: str
        :type B: str
        :rtype: int
        """
        na, nb = len(A), len(B)
        times = nb / na + 3
        for i in range(1, times):
            if B in A * i:
                return i
        return -1

1 2 3 4 5 6 7 8 9 10 11 12 13

C++版本如下:

class Solution {
public:
    int repeatedStringMatch(string A, string B) {
        int NA = A.size(), NB = B.size();
        int times = NB / NA + 3;
        string t = A;
        for (int i = 1; i < times; i++) {
            if (t.find(B) != string::npos) return i;
            t += A;
        }
        return -1;
    }
};

1 2 3 4 5 6 7 8 9 10 11 12 13

参考资料:

[LeetCode] Repeated String Match 重复字符串匹配open in new window

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发