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. PHP中的生成器

    python中有的,php一样不落下呀. <?php //生成器 //生成器代理 //生成器返回表达示 function gen1() { yield '1'; yield '2'; yield ...

  2. 说说openjdk及G1回收器日志内容详细分析

    谈谈openjdk: 在正式往下学习JVM之前,这里谈谈openjdk这个网站,这个在学习java并发时也用过它来分析过锁的底层实现,如:https://www.cnblogs.com/webor20 ...

  3. Linux查看打日志文件

    1.如果文件比较小的话,使用vim直接查看,如果文件比较大的话,使用vim会直接卡主 2.如果想要查看正在滚动的日志文件.这个命令可以查看大文件. tail -f file Ctrl+c 终止tail ...

  4. 配置/更改vue项目中的资源路径

    打开 build/webpack.base.conf.js  ,在module.exports .resolve.alias下自定义: alias: { 'vue$': 'vue/dist/vue.e ...

  5. 线程的等待与唤醒,实现if...else里面的值交互依次输出

    线程通信原理图: 资源类: package com.yonyou.sci.gateway.exec.threadnet; public class Resource { String name; St ...

  6. java去除字符串中的特殊符号或指定的字符

    String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}[]‘::”“’., .? ...

  7. ios Aspects面向切面沉思录—面向结构编程—面向修改记录编程—面向运行时结构编程—元编程?

    1.将主功能看成一个巨大的结构: 2.将切面注入的交叉业务看成是一组结构修改的注册:目标对象+方法是修改的键值: 3.Aspects引擎是修改的执行者.记录者.和维护者: 4.函数和方法是它操作和面对 ...

  8. Response Assertion(响应断言)

    Response Assertion(响应断言) 响应断言是对服务器的响应数据进行规则匹配. Name(名称):可以随意设置,最好有业务意义. Comments(注释):可以随意设置,可以为空. Ap ...

  9. UFUN函数 UF_ATTR函数(UF_ATTR_read_value 函数用法)

    //此函数的功能是输入tag值,返回与属性标题对应的属性值 static string read_attr(tag_t object_tag) { UF_initialize(); ]="零 ...

  10. [RN] React Native 仿美团下拉筛选菜单控件

    React Native 仿美团下拉筛选菜单控件 演示效果如下: 使用方法如下: 1.安装 npm install react-native-dropdownmenus --save react-na ...