LeetCode-459. Repeated Substring Pattern

网友投稿 249 2022-08-29

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: TrueExplanation: It's the substring "ab" twice.

Example 2:

Input: "aba"Output: False

Example 3:

Input: "abcabcabcabc"Output: TrueExplanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

题解:

class Solution {public: bool repeatedSubstringPattern(string s) { int n = s.length(); if (n < 2) { return false; } for (int i = 1; i <= n / 2; i++) { if (n % i == 0) { string subs = s.substr(0, i); for (int k = i; k < n; k += i) { string nextSubs = s.substr(k, i); if (subs != nextSubs) { break; } if (k + i == n) { return true; } } } } return false; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-347. Top K Frequent Elements
下一篇:小游戏成私域营销新风口,这家公司抢先入局帮品牌拉新促活!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~