leetcode 459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Example 1:
Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.
Example 2:
Input: "aba"
Output: False
Example 3:
Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
思路:假设子串长度为k,那么原串s的长度len为n*k(n>=2),那么s的next数组,由于对称性会在下标k+1起开始逐一递增。直到增加到len-k,如果next[len]%k==0代表s由整数个长度为k的子串构成。
class Solution {
public:
bool repeatedSubstringPattern(string s) {
int i, j;
int m = s.size();
vector<int> next(m);
for (i = 1, j = 0; i < m; ++i) {
while(j > 0 && s[i] != s[j]) j = next[j-1];
if (s[i] == s[j]) ++j;
next[i] = j;
}
int k = m - next[m-1];
return next[m-1]&&next[m-1]%k==0;
}
};
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号:
![](https://images.cnblogs.com/cnblogs_com/pk28/1069498/o_qrcode_for_gh_db07a03aaa56_344.jpg)
分类:
Leetcode
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库