LeetCode_459. Repeated Substring Pattern
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: 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.)
package leetcode.easy;
public class RepeatedSubstringPattern {
public boolean repeatedSubstringPattern(String s) {
// corner case
if (s == null || s.length() == 0) {
return true;
}
int n = s.length();
for (int len = n / 2; len >= 1; len--) {
if (n % len != 0) {
continue; // s length must can be divided by the pattern length
} else {
String pattern = s.substring(0, len); // pattern string
int i = len; // start index of 2nd pattern
int j = i + len - 1; // end index of 2nd pattern
while (j < n) {
String substr = s.substring(i, j + 1);
if (!pattern.equals(substr)) {
break; // failed for this pattern, try next pattern
} else {
i += len;
j += len;
}
}
// if it past the last substring check, i will be n
if (i == n) {
return true;
}
}
}
return false;
}
@org.junit.Test
public void test() {
System.out.println(repeatedSubstringPattern("abab"));
System.out.println(repeatedSubstringPattern("aba"));
System.out.println(repeatedSubstringPattern("abcabcabcabc"));
}
}
LeetCode_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 ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...
- *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 ...
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- Repeated Substring Pattern Leetcode
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 ...
随机推荐
- Win10上的Docker应用:Hello World
前言: 最近学习了Docker相关技术点,国内关于Docker的资料大多是基于Linux系统的,但是我对Linux又不熟(实际上没用过,掩面哭笑.Jpg). 好在在Win10下也是支持Docker的, ...
- 201671030107 胡文艳 实验十四 团队项目评审&课程项目总结
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十四 团队项目评审&课程学习总结 作业学习目标 1.掌握软件项目评审会议流程 ...
- 项目Beta冲刺(5/7)(追光的人)(2019.5.27)
所属课程 软件工程1916 作业要求 Beta冲刺博客汇总 团队名称 追光的人 作业目标 描述Beta冲刺每日的scrum和PM报告两部分 队员学号 队员博客 221600219 小墨 https:/ ...
- 20180429模拟赛T1——添边问题
[问题描述] 没有环的有向图称为有向无环图,这是一个多么美好的结构吖. 如果有一张有 N 个点的有向图,我们可能需要删掉一些边使它变成一张有向无环图.假设初始时我们只有 N 个互不相连的点,当然它也是 ...
- js 递归 汉诺塔的例子
程序调用自身的编程技巧称为递归. //汉诺塔的游戏,n为圆盘编号数量,编号,a,b,c代表的是三个柱子 var hanio=function(n,a,b,c){ if(n>0){ ...
- Is Safari on iOS 6 caching $.ajax results? post Cache
https://stackoverflow.com/questions/12506897/is-safari-on-ios-6-caching-ajax-results Since the upgra ...
- python--模块学习之xml模块
xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 本文主要学习的ElementTree是python的XML处理模块,它提供了一个轻量级的对象 ...
- learning shell monitor prog function
[Purpose] Shell script monitor prog function [Eevironment] Ubuntu 16.04 bash env [ ...
- 文件搜索命令(命令搜索)which、whereis
一.which命令: 搜索命令所在目录(绝对路径)或者别名信息. 用户可以使用的命令存放在: /bin /usr/bin 管理员使用的命令: /sbin /usr/sbin 1.带有别名的命令: 二. ...
- Netflix-mantis 实时数据流开发平台
mantis 是netflix 开源的已经在netflix 使用了多年的实时流处理平台,目前从官方文档的介绍,在netflix使用场景很多 使用场景 上下文报警 监控netflix 的微服务 异常追踪 ...