[leetcode] 459. Repeated Substring Pattern
来源:互联网 发布:软件开发学校好吗 编辑:程序博客网 时间:2024/06/08 15:55
Qustion:
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.)
Solution:
先想着把整个字符串分成两部分(如果可以的话),看这两部分是否相等;若否,则再分成三部分(如果可以),看这三部分是否相等;如此类推,一直到分成s.size()
部分,每部分一个字符,看是否相等;若不等,则返回false。
helper函数用于判断多个部分是否相等,逐个字符逐个字符判断。
class Solution {public: bool repeatedSubstringPattern(string s) { for (int i = 2; i <= s.size(); i++) { if (s.size() % i == 0 && helper(s, s.size()/i)) return true; } return false; } bool helper(string & s, int k) { int i = k, j = 0; while (i < s.size()) { if (s[i++] != s[j++]) return false; if (j == k) j = 0; } return true; }};
阅读全文
0 0
- 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
- HDU 5834 Magic boy Bi Luo with his excited tree——树形dp
- Ebean ORM功能介绍
- Maven项目Run As无Run On Server的解决方法
- 1119: 一维数组排序
- Python OpenCV图像模板匹配
- [leetcode] 459. Repeated Substring Pattern
- JD采销端数据产品介绍
- Linux下直接安装jdk的命令
- mysql 分区分表
- swagger注释API详细说明
- Makefile的结构和使用
- Object Detection系列(三) Fast R-CNN
- Java data
- map集合的遍历