资讯专栏INFORMATION COLUMN

[LeetCode] 686. Repeated String Match

ashe / 641人阅读

Problem

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.

Solution
class Solution {
    public int repeatedStringMatch(String A, String B) {
        if (A.length() == 0 || B.length() == 0) return -1;
        StringBuilder sb = new StringBuilder();
        int count = 0;
        while (sb.length() < B.length()) {
            sb.append(A);
            count++;
        }
        if (sb.toString().indexOf(B) != -1) return count;
        if (sb.append(A).toString().contains(B)) return count+1;
        return -1;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71960.html

相关文章

  • 459. Repeated Substring Pattern

    摘要:题目链接利用求数组的方法来做,利用自身的重复性,表示在中,最大的使得参考视频所以如果这个是呈现的样子的话,假设的大小是,则并且根据可知,一旦中间出现不满足的情况,,所以必然不是的,如果结尾处少了的话,例如,虽然,但 459. Repeated Substring Pattern 题目链接:https://leetcode.com/problems... 利用kmp求prefix数组的方...

    Y3G 评论0 收藏0
  • [LeetCode] 10. Regular Expression Matching

    Problem Given an input string (s) and a pattern (p), implement regular expression matching with support for . and *. . Matches any single character. * Matches zero or more of the preceding element. Th...

    mo0n1andin 评论0 收藏0
  • leetcode187. Repeated DNA Sequences

    摘要:题目要求所有的都是有这四个字母组成的,比如。这个问题要求我们在一个序列中找到出现超过两次的长度为的子序列。因为个字母意味着每个字母至少需要位才能表示出来。因为每个字符串对应的二进制长度为,小于整数的,因此是可行的。 题目要求 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for...

    Noodles 评论0 收藏0
  • [Leetcode] Repeated DNA Sequences 重复DNA序列

    摘要:哈希表法复杂度时间空间思路最简单的做法,我们可以把位移一位后每个子串都存入哈希表中,如果哈希表中已经有这个子串,而且是第一次重复,则加入结果中。如果哈希表没有这个子串,则把这个子串加入哈希表中。 Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A, C, G, a...

    wing324 评论0 收藏0
  • Leetcode PHP题解--D4 961. N-Repeated Element in Size

    摘要:一般算法题用数学上的定义方法去描述问题,所以理解起来可能费劲一些。其中,数字为数组的长度的一半。求元素出现次数函数。输出用函数,从函数的返回中,查找数字。 961. N-Repeated Element in Size 2N Array 题目链接 961. N-Repeated Element in Size 2N Array 题目分析 在长度为2N的数组A中,有N+1个元素。其中恰好...

    opengps 评论0 收藏0

发表评论

0条评论

ashe

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<