459. Repeated Substring Pattern

Easy

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的更多相关文章

  1. 43. leetcode 459. Repeated Substring Pattern

    459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...

  2. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  3. LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

    459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...

  4. *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 ...

  5. 459. Repeated Substring Pattern

    https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...

  6. [LeetCode] Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  7. Repeated Substring Pattern Leetcode

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  8. LeetCode算法题-Repeated Substring Pattern(Java实现)

    这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...

  9. [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 ...

随机推荐

  1. 图论 - Bellman-Ford算法

    Bellman-Ford Dijkstra算法虽好,但是不能解决带有负边权的图. 而利用Bellman-Ford可以完美的解决最短路和负边权的问题 朴素Bellman-Ford算法 w[i] 权值 u ...

  2. Redis的入门

    什么是NOSQL? NOSQL(Not Only SQL)不仅仅是数据库,是一种全新的理念,泛指非关系型的数据库. 为什么需要NOSQL? 随着互联网的高速崛起,网站的用户群的增加,访问量的上升,传统 ...

  3. Gym100739H Hard Molecules

    Hard Molecules 给定一个连通图中每个点的度数,求一个满足条件的图,图可以有重边,不能有自环. n<=5000, di<=109 题解 如果不要求图连通,那么只需要判断 \[ ...

  4. maven中jar冲突解决

    Maven中jar包冲突是开发过程中比较常见而又令人头疼的问题,我们需要知道 jar包冲突的原理,才能更好的去解决jar包冲突的问题.本文将从jar包冲突的原理和解决两个方面阐述Maven中jar包冲 ...

  5. python-自动登录禅道

    from bs4 import BeautifulSoup import hashlib import requests import re from tool.request_data_change ...

  6. objc_msgSend method_getTypeEncoding 与 @encode

    struct objc_method { SEL _Nonnull method_name                                 OBJC2_UNAVAILABLE; cha ...

  7. Xcode 创建.a和framework静态库(转)

    最近因为项目中的聊天SDK,需要封装成静态库,所以实践了一下创建静态库的步骤,做下记录. 库介绍 库从本质上来说是一种可执行代码的二进制格式,可以被载入内存中执行.库分静态库和动态库两种.iOS中的静 ...

  8. luogu P3799 妖梦拼木棒

    二次联通门 : luogu P3799 妖梦拼木棒 /* luogu P3799 妖梦拼木棒 用一个桶存下所有的木棒 美剧两根短的木棒长度 后随便乘一乘就 好了.. */ #include <a ...

  9. 64位下的InlineHook

    目录 x64下手工HOOK的方法 一丶HOOK的几种方法之远跳 1. 远跳 不影响寄存器 + 15字节方法 2.远跳 影响寄存器 + 12字节方法 3.影响寄存器,恢复寄存器 进行跳转. 4. 常用 ...

  10. java自动化配置工具 - autoconfig 简介

    对于java程序员来说各种各样的配置文件是司空见惯的,比如spring的bean配置,struts的action配置等等.有些配置会随着运行环境的变化而各不相同,最典型的就是jdbc驱动的配置,在开发 ...