leetcode 686. Repeated String Match
来源:互联网 发布:csgo弹道优化参数脚本 编辑:程序博客网 时间:2024/05/01 22:34
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.
直接使用C++的find函数做查找即可,当A的长度小于B的时候,我们可以先进行重复A,直到A的长度大于等于B,并且累计次数cnt。那么此时我们用find来找,看B是否存在A中,如果存在直接返回cnt。如果不存在,我们再加上一个A,再来找,这样可以处理这种情况A=”abc”, B=”cab”,如果此时还找不到,说明无法匹配,返回-1,
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <numeric>#include <cmath>#include <regex>using namespace std;class Solution {public: int repeatedStringMatch(string a, string b) { int count = 1; string aa = a; while (aa.length() < b.length()) { count++; aa += a; } if (aa.find(b) != aa.npos) return count; else { aa += a; if (aa.find(b) != aa.npos) return count + 1; else return -1; } }};
阅读全文
0 0
- leetcode 686. Repeated String Match
- leetcode 686. Repeated String Match
- leetcode 686. Repeated String Match
- 【LeetCode】686. Repeated String Match
- leetcode 686. Repeated String Match
- Leetcode-686. Repeated String Match(string)
- Leetcode | Repeated String Match
- 686. Repeated String Match
- 686. Repeated String Match
- leetcode 686 Repeated String Match
- leetcode题解-686. Repeated String Match && 38. Count and Say
- [LC]686. Repeated String Match
- LWC 52:686. Repeated String Match
- LeetCode Weekly Contest 52 Repeated String Match(string)
- Leetcode挑战题——Repeated String Match
- Repeated String Match问题及解法
- leetcode_686.Repeated String Match ? 待解决
- Leetcode Repeated DNA Sequences
- 《微服务架构与实践》读书笔记
- 经典算法之数串
- IObit Smart Defrag Pro(磁盘碎片整理工具)官方中文注册版V5.8.0下载 | smart defrag 5激活码
- 将app放到服务器上让用户下载
- 实时操作系统任务调度 DMA 中断向量表和中断处理 Memory
- leetcode 686. Repeated String Match
- 除了美团点评,商家如何获客
- oracle建表
- Map.Entry使用详解
- SQL CREATE TABLE
- 二分图匹配
- MySQL中的两种临时表
- 原生js根据url参数动态显示静态页面内容
- mysql存储过程,生成随机字符串