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 ...
随机推荐
- 项目Beta冲刺(团队) --3/7
课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺 团队名称:葫芦娃队 作业目标:进行新一轮的项目冲刺,尽力完成并完善项目 团队博客 队员学号 队员昵称 博客地址 04160242 ...
- crontab每小时运行一次(转)
https://blog.csdn.net/liu0808/article/details/80668705 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错 ...
- mcp2515屏蔽寄存器和过滤寄存器的学习
mcp2515是can控制器,简单的来讲,就是只要配置好寄存器,芯片就能够自动的解析can数据帧,同时保存到接收缓存中,提醒单片机可以读取can的数据字节. 读取的方式是快速spi,可以达到10Mbi ...
- win10系统绑定本地IP和mac地址
第一步:找到自己的IP和mac地址 1.按着win键+R键,输入cmd(大小写都一样) 2.命令: ipconfig /all #查看所有地址 然后按“回车键” 3.这样 ...
- MVC框架与增强
一.什么是MVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示 ...
- 你真的知道Java中boolean类型占用多少个字节吗?
为什么要问这个问题,首先在Java中定义的八种基本数据类型中,除了其它七种类型都有明确的内存占用字节数外,就boolean类型没有给出具体的占用字节数,因为对虚拟机来说根本就不存在 boolean 这 ...
- IIS服务器部署web应用《一》
最近了解到开发用iis部署环境,于是了解了下. IIS用于部署web应用,其简单,配置方便,可以用作本地机器作为服务器进行部署.且所在部署系统为windows,便于使用iis. 端口80需要修改为别的 ...
- 使用Lua脚本通过原子减防止超卖
需求 双十二要搞一个一分钱门票抢购的活动. 分析 性能分析,抢购时会发生高并发,如果仅仅依靠Mysql数据库,有可能因为大量的请求频繁访问数据库造成服务器雪崩,所以考虑通过Redis减库存,最终的数据 ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- Map、Set、List是否有序
首先我们应该清楚这个概念:这里的有序和无序不是指集合中的排序,而是是否按照元素添加的顺序来存储对象. list是按照元素的添加顺序来存储对象的,因此是有序的.他的实现类ArrayList.Linked ...