https://leetcode.com/contest/leetcode-weekly-contest-52/problems/repeated-string-match/




Paste your text #include <iostream>
#include <string>

using namespace std;

class Solution {
public:
    int repeatedStringMatch(string A, string B) {
        int a = A.length();
        int b = B.length();
        string tmp = A;
        if (!a)
            return 0;
        if (!b)
            return 1;
        int res = -1;
        int repeat = (b / a) + 1;
        while (repeat-- > 0) {
            tmp = tmp + A;
        }
        for (int i = 0; i < a; i++) {
            if (tmp.compare(i, b, B) == 0) {
                res = (((i + b) / a) + ((((i + b) % a) != 0) ? 1 : 0));
                return res;
            }
        }
        return res;
    }
};

int main()
{
    string A = "abcd"; string B = "cdabcdab";
    Solution sol;
    int res = sol.repeatedStringMatch(A, B);
    printf("res = %d\n", res);
    return 1;
}here.

留言

這個網誌中的熱門文章

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/longest-univalue-path/

https://leetcode.com/contest/leetcode-weekly-contest-52/problems/maximum-sum-of-3-non-overlapping-subarrays/