Repeated Substring Pattern Leetcode
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.)
有repeat pattern的话整个string可以分成几个相同的部分,可以被某一个数整除。
先找出这个数,从str.length()/2开始。
如果某个数可以被整除,这个数就是repeat pattern的长度, str.length/这个数就是repeat pattern重复的次数。
如果次数 * repeat pattern等于原数组,就说明原数组是repeat string组成的string。
public class Solution {
public boolean repeatedSubstringPattern(String str) {
if (str == null) {
return false;
}
for (int i = str.length() / 2; i >= 1; i--) {
if (str.length() % i == 0) {
StringBuilder s = new StringBuilder();
int times = str.length() / i;
for (int j = 0; j < times; j++) {
s.append(str.substring(0, i));
}
if (s.toString().equals(str)) {
return true;
}
}
}
return false; }
}
教训是不要被实习的内容所累。。。这个算法由于题目条件限制变得很简单。。。
Repeated Substring Pattern Leetcode的更多相关文章
- LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- LeetCode_459. Repeated Substring Pattern
459. Repeated Substring Pattern Easy Given a non-empty string check if it can be constructed by taki ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- *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 ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- LeetCode算法题-Repeated Substring Pattern(Java实现)
这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...
- [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 ...
随机推荐
- Java中关于HashMap的使用和遍历(转)
Java中关于HashMap的使用和遍历 分类: 算法与数据结构2011-10-19 10:53 5345人阅读 评论(0) 收藏 举报 hashmapjavastringobjectiterator ...
- IMCP网际控制协议
IP协议是TCP/IP协议使用的在网络层传输机制,它是一种不可靠的无连接的数据报协议,但是IP协议假定了底层是不可靠的,因此,要尽最大的努力传输到目的地,但正因为如此,IP协议则没有了保证,也就是说, ...
- Codeforces Round #367 (Div. 2)D. Vasiliy's Multiset (字典树)
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- 圖片裁剪大頭貼功能 - ASP.NET WebForm + jQuery + imgAreaSelect
系統操作環境: ASP.NET WebForm .NET Framework 4.0 (C#) jQuery 1.7.1 imgAreaSelect 0.9.8 目錄結構: 與之前使用ASP.NET ...
- redis对比memcached
摘要 : 简单地比较Redis与Memcached的区别,大多数都会得到以下观点: 1 Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储. 2 Re ...
- iOS JsonModel
iOS JsonModel 的使用 本文转自:http://blog.csdn.net/smking/article/details/40432287 下面讲一下JSONModel的使用方法. @in ...
- NodeJs与ActionScript的GET和POST通讯
今天项目遇到一个小问题,做了个小功能,向服务端发送GET请求,但是服务端解析数据是时候报语法错误,如下: make Request data error:SyntaxError: Error #113 ...
- ios中操作技巧
1.配置字段快捷键: @property(nonatimic,copy) NSString *<#param#>; 2.NSNumber 转NSString 最快简单方式: NSNumbe ...
- 【POJ2104】【HDU2665】K-th Number 主席树
[POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...
- iOS开发实现Label中多颜色多字体
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(8, 100, 300, 30)]; label.textColor = wor ...