题意

题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A?

暴力解法

直接 A+A+...,到哪次 A 包含 B 了,就返回 A 的个数。

但是 B 也可能不是 A 的拼接的子字符串,所以这种直观解法还是存在隐患(无限循环),最好还是动动脑筋。

动脑筋解法

假如 B 的长度为 b,A 的长度为 a,那么 n=Math.ceil(b/a) 一定意味着什么。但是到底 n 意味着什么呢?看看例子先。

假设 A=“abcdefg”, B="gab", n=Math.ceil(3/7) = 1, 这种情况下,B 是 A+A 的子字符串,也就是答案为 2,为 n+1

假设 A=“ab”,B="abababa",n=Math.ceil(7/2) = 4,这种情况下,B 是 A+A+A+A 的子字符串,也就是答案为 4,为 n

从上面两种情况看,答案通常为 n 或者 n+1。但是这两种情况是否涵盖了所有的情况呢?我们无法肯定。

我们可以提出假设,假设最终答案最小为 n(这一点很容易证明,不然 B 比 A 长,哪里能成为 A 的子字符串),最大为 n+1

那么我们只需要证明第二点,通过反推,证明 答案大于等于 n+2 的时候,存在累赘的 A  就可以了。

如下图,假设 B 的长度是 A 的长度的 n 倍小一点。

如果答案为 n+2 的话,那么 B 在 A 的拼接字符串中的匹配情况应该如下图所示:

或者是

第一种情况,最前面那个 A 是多余的;第二种情况,最后面那个 A 是多余的。由此可以得出,如果存在答案,答案不会超过 n+1。

结论

这道题答案存在三种情况,假设 n = Math.ceil( B.length/A.length ),则

1. A 怎么拼接都不可能成为 B 的母串;

2. 答案为 n;

3. 答案为 n+1。

代码就不提供了,按照这种思路,很容易就可以解答出来的。

【刷题笔记】686. Repeated String Match的更多相关文章

  1. 【Leetcode_easy】686. Repeated String Match

    problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...

  2. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 686. Repeated String Match 字符串重复后的子字符串查找

    [抄题]: Given two strings A and B, find the minimum number of times A has to be repeated such that B i ...

  4. Leetcode 686 Repeated String Match

    Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...

  5. [LeetCode] 686. Repeated String Match 重复字符串匹配

    Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...

  6. 686. Repeated String Match

    方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...

  7. 【leetcode刷题笔记】Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  8. 【leetcode刷题笔记】Scramble String

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  9. 686. Repeated String Match判断字符串重复几次可以包含另外一个

    public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...

随机推荐

  1. Java并发编程:Concurrent锁机制解析

    Java并发编程:Concurrent锁机制解析 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: # ...

  2. What is one-hot?

    SEO: libtorch 如何 OneHot ? torch OneHot 源代码 ? https://www.tensorflow.org/api_docs/python/tf/one_hot 最 ...

  3. qt 如何使用私有类?

    在模块后面加上 -private 关键字,例如 widgets-private 然后 qmake 让配置文件运行一下. 在使用 QStackedLayout 布局并设置 QStackedLayout: ...

  4. activity 生命周期 http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for

    331down voteaccepted See it in Activity Lifecycle (at Android Developers). onCreate(): Called when t ...

  5. 在Intellij上开发项目发布到tomcat时,同一个局域网内的其他机子访问不到自己电脑上tomcat中的项目,只能本机访问

    在Intellij上开发项目发布到tomcat时,同一个局域网内的其他机子访问不到自己电脑上tomcat中的项目,只能本机访问 问题描述:在Intellij上开发项目发布到tomcat时,同一个局域网 ...

  6. Algorithms4th 1.1.25 欧几里得算法——数学归纳法证明

    欧几里得算法的自然语言描述 计算两个非负整数p和q的最大公约数: 若q是0,则最大公约数为p.否则将p除以q得到余数r,p和q的最大公约数即为q和r的最大公约数. 数学归纳法证明 基础步骤: 若q = ...

  7. IBM IMM默认ID

    默认ID: http://192.168.70.125用户名:USERID密码:PASSW0RD (数字0)

  8. win10 护眼

  9. Sass函数:数字函数-round()函数

    round() 函数可以将一个数四舍五入为一个最接近的整数: >> round(12.3) 12 >> round(12.5) 13 >> round(1.4999 ...

  10. JVM、JRE、JDK的区别

    什么是Java虚拟机(JVM)?为什么Java被称作是"平台无关的编程语言"? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行 ...