【刷题笔记】686. Repeated String Match
题意
题目大意是,给两个字符串 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的更多相关文章
- 【Leetcode_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- [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 ...
- 686. Repeated String Match
方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...
- 【leetcode刷题笔记】Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 【leetcode刷题笔记】Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 686. Repeated String Match判断字符串重复几次可以包含另外一个
public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...
随机推荐
- from、includes、indexOf
from.includes.indexOf:https://blog.csdn.net/j59580/article/details/53897630?utm_source=blogxgwz1 语法 ...
- LeetCode #807. Max Increase to Keep City Skyline 保持城市天际线
https://leetcode-cn.com/problems/max-increase-to-keep-city-skyline/ 执行用时 : 3 ms, 在Max Increase to Ke ...
- 洛谷 P1440 求m区间内的最小值(单调队列)
题目链接 https://www.luogu.org/problemnew/show/P1440 显然是一道单调队列题目…… 解题思路 对于单调队列不明白的请看这一篇博客:https://www.cn ...
- PHP实现上传文件到服务器
<?php /**************************** *** 功能:上传文件到服务器 ****************************/ session_start() ...
- 同一客户端多个git账号的配置
同一客户端多个git账号的配置 同一客户端多个git账号的配置 步骤一:用ssh-keygen命令生成一组新的id_rsa_new和id_rsa_new.pub. 1 ssh-keygen -t rs ...
- 云中沙箱学习笔记1-快速部署并使用MySQL数据库
1.1 背景知识 业务背景 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle旗下产品.MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面MyS ...
- JS 页面繁简字转换
// 本js用于客户在网站页面选择繁体中文或简体中文显示,默认是正常显示,即简繁体同时显示// 在用户第一次访问网页时,会自动检测客户端语言进行操作并提示.此功能可关闭// 本程序只在UTF8编码下测 ...
- linux性能分析工具Memory
- standard_key.kmp
[KeyRemap]keyVersion=2B33554467=[eraseeof]S36=[bof]B33554466=[pagedn]S35=[eof]B33554465=[pageup]B10= ...
- c++消息中间件
ZeroMQ ActiveMQ-CPP 另外 ZeroMQ 的作者用 C 重构了一套.改名叫:nanomsg ZeroMQ:https://www.cnblogs.com/rainbowzc/p/33 ...