LeetCode-459. Repeated Substring Pattern
来源:互联网 发布:ktv怎么唱歌才好听知乎 编辑:程序博客网 时间:2024/04/29 02:17
问题:https://leetcode.com/problems/repeated-substring-pattern/?tab=Description
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. 给定一个没有空格的的字符串,查看该字符串是否由其中的一个子串重复多次得到。给定的字符串中只有小写的英文字母,而且长度不超过10000.
Example 1: Input: “abab” Output: True Explanation: It’s the substring “ab” twice.
例子 1: 输入: “abab” 输出: True 解释:这是由子串”ab”重复得到
Example 2: Input: “aba” Output: False
例子 2: 输入: “aba” 输出: False
Example 3: Input: “abcabcabcabc” Output: True
Explanation: It’s the substring “abc” four times. (And the substring “abcabc” twice.)
例子3 : 输入: “abcabcabcabc” 输出: True
解释:这是由子串”abc”重复四次得到的,也可以认为是”abcabc重复两次得到的”
分析:原字符串长度为l。要求是否是重复子串,如果有重复的,子串长度一定小于原串的一半。首先求得子串长度,如果某两个字符一样,可以先假设它们之间的距离就是子串长度p。然后看相隔p个长度的是否都是一样的数字。如果都是,那么判定的最后一个位置一定是l-p-1的位置。这样可以返回true。如果不是则继续找。
C++代码:
class Solution {public: bool repeatedSubstringPattern(string s) { int l=s.size(); for(int p=1;p<l/2+1;p++){ for(int i=0;i<l-p;i++){ if(s[i] !=s[i+p]) break; if(l%p==0 && i==l-p-1) return true; } } return false; }};
- LeetCode 459. Repeated Substring Pattern
- Leetcode-459. Repeated Substring Pattern
- leetcode 459. Repeated Substring Pattern
- LeetCode 459. Repeated Substring Pattern
- 【LeetCode】 459. Repeated Substring Pattern
- LeetCode 459. Repeated Substring Pattern
- Leetcode 459. Repeated Substring Pattern
- LeetCode-459. Repeated Substring Pattern
- Leetcode 459. Repeated Substring Pattern
- 【Leetcode】459. Repeated Substring Pattern
- leetcode:459. Repeated Substring Pattern
- [LeetCode]459. Repeated Substring Pattern
- LeetCode 459. Repeated Substring Pattern
- LeetCode 459. Repeated Substring Pattern
- Leetcode 459. Repeated Substring Pattern
- LeetCode-459. Repeated Substring Pattern
- [LeetCode]459. Repeated Substring Pattern
- leetcode 459. Repeated Substring Pattern
- 线程池
- gcc/g++编译c/c++文件
- linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)
- 软件工程基本知识
- Mybatis SqlSessionFactory创建过程
- LeetCode-459. Repeated Substring Pattern
- 217. Contains Duplicate
- OpenJudge Noi 3532 最大上升子序列和(dp)
- http基础知识
- 互联网协议(五层模型)
- 距离矢量算法的缺点
- Spark操作—aggregate、aggregateByKey详解
- OpenCV3.0与VisualStudio2013配置
- jquery第一季(概述,基本选择器与层次选择器)