给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
示例 1:
输入: "abab"
输出: True
解释: 可由子字符串 "ab" 重复两次构成。

示例 2:
输入: "aba"
输出: False

示例 3:
输入: "abcabcabcabc"
输出: True
解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)
详见:https://leetcode.com/problems/repeated-substring-pattern/description/

C++:

class Solution {
public:
bool repeatedSubstringPattern(string str)
{
int n = str.size();
for (int i = n / 2; i >= 1; --i)
{
if (n % i == 0)
{
int c = n / i;
string t = "";
for (int j = 0; j < c; ++j)
{
t += str.substr(0, i);
}
if (t == str)
{
return true;
}
}
}
return false;
}
};

参考:https://www.cnblogs.com/grandyang/p/6087347.html

459 Repeated Substring Pattern 重复的子字符串的更多相关文章

  1. Leetcode459.Repeated Substring Pattern重复的子字符串

    给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释 ...

  2. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  3. 43. leetcode 459. Repeated Substring Pattern

    459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...

  4. 【LeetCode】459. Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  5. LeetCode 459 Repeated Substring Pattern

    Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...

  6. *459. Repeated Substring Pattern (O(n^2)) two pointers could be better?

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  7. 459. Repeated Substring Pattern

    https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...

  8. [LeetCode] 459. Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  9. Repeated Substring Pattern --重复字符串

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

随机推荐

  1. Office EXCEL 如何将复制的一堆数据按空格断开

    1 复制粘贴一堆数据,点击数据-分类,然后点击下一步   2 一直下一步   3 最后效果如下图所示  

  2. 【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】

    [066-Plus One(加一)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a non-negative number represented as ...

  3. Mapreduce运行过程分析(基于Hadoop2.4)——(三)

    4.4 Reduce类 4.4.1 Reduce介绍 整完了Map,接下来就是Reduce了.YarnChild.main()->ReduceTask.run().ReduceTask.run方 ...

  4. Pierce振荡器设计

    一.Pierce振荡器电路 Inv:内部反相器,作用等同于放大器: Q:石英晶体或陶瓷晶振: RF:内部反馈电阻(使反相器工作在线性区): RExt:外部限流电阻(防止石英晶体被过分驱动): CL1. ...

  5. C语言的各种位运算符的操作简述

    运算符: 算术运算符: + , - , * , / , % , ++ , -- 符合运算符: += , *= , /= , %= 条件运算符:  ? : 关系运算符: == , >= , < ...

  6. Receiver type ‘X’ for instance message is a forward declaration

    这往往是引用的问题. ARC要求完整的前向引用,也就是说在MRC时代可能仅仅须要在.h中申明@class就能够,可是在ARC中假设调用某个子类中未覆盖的父类中的方法的话.必须对父类.h引用,否则无法编 ...

  7. hdu 2059 龟兔赛跑 (dp)

    /* 把起点和终点比作加油站,那总共同拥有n+2个加油站了, 每次都求出从第0个到第j个加油站(j<i)分别在加满油的情况下到第i个加油站的最短时间dp[i], 终于的dp[n+1]就是最优解了 ...

  8. [办公应用]让WORD自动显示到四级目录

    一般情况下,word的目录默认显示到三级目录.如果需要显示到四级目录,你会怎么操作呢? 只要按下图所示,单击“插入”-“引用”-“索引和目录” 然后单击“目录”选项卡,将“显示级别”处的3改为4即可. ...

  9. python和python3

    1 安装python和python3的方法 如果是python,那么直接python setup.py install; 如果是python3,那么直接python3 setup.py install ...

  10. 系统队列中的Windows错误报告