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.)

Solution: O(n^2)

create different gaps. Hard part is to check if they are repeated substring with fixed number (boundary cased needed to be care)

class Solution {
boolean check(String s, int i){//check each gap
int j = 0;
String temp1 = s.substring(j,j+i); // define temp1
while(j+i<s.length()){
//System.out.println(temp1+j+i);
j = j+i;//move the gap distance
if(j+i>s.length()) break;
String temp2 = s.substring(j, j+i);//compare the String
if(!temp1.equals(temp2)) return false;
temp1 = temp2;
//if(j+i==s.length()) break;
}
if(j+i==s.length()) return true;
return false;
}
public boolean repeatedSubstringPattern(String s) {
if(s==null || s.length() == 1) return false;
for(int i = 1; i<=s.length()/2; i++){
if(s.length()%i!=0) continue;
//System.out.println(i);
//int j = 0;
if(check(s, i)) return true;
}
return false;
}
}

In a String, check if "ababab" 's substring has repested pattern

compare each character.

class Solution {
boolean check(String s, int i){//check each gap
int j = 0;
while(j+i < s.length()){
if(s.charAt(j) != s.charAt(j+i)) return false;//copare each characters !!!!!!!!!!!!!!!
j++;
}
return true;
}
public boolean repeatedSubstringPattern(String s) {
if(s==null || s.length() == 1) return false;
for(int i = 1; i<=s.length()/2; i++){
if(s.length()%i!=0) continue;
//System.out.println(i);
//int j = 0;
if(check(s, i)) return true;
}
return false;
}
}

Solution 3 (O(n)) using build in function and simple tricks

public boolean repeatedSubstringPattern(String s) {
StringBuilder str = new StringBuilder(s + s); //concatenate the given string with itself
str.deleteCharAt(0); //remove first char
str.deleteCharAt(str.length() - 1); //remove last char return str.indexOf(s) !=-1? true: false; //check if the given string is substring of the new string
}

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.)

*459. Repeated Substring Pattern (O(n^2)) two pointers could be better?的更多相关文章

  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. 459. Repeated Substring Pattern

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

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

  5. LeetCode 459 Repeated Substring Pattern

    Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...

  6. 【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 ...

  7. KMP - LeetCode #459 Repeated Substring Pattern

    复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...

  8. LeetCode - 459. Repeated Substring Pattern - O(n)和O(n^2)两种思路 - KMP - (C++) - 解题报告

    题目 题目链接 Given a non-empty string check if it can be constructed by taking a substring of it and appe ...

  9. 459. Repeated Substring Pattern 判断数组是否由重复单元构成

    [抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and append ...

随机推荐

  1. day32 线程

    1.     线程是什么,有了进程为什么还要线程 进程有很多优点,它提供了多道编程,让我们感觉我们每个人都拥有自己的CPU和其他资源,可以提高计算机的利用率.很多人就不理解了,既然进程这么优秀,为什么 ...

  2. 单元测试mock框架——jmockit实战

    JMockit是google code上面的一个java单元测试mock项目,她很方便地让你对单元测试中的final类,静态方法,构造方法进行mock,功能强大.项目地址在:http://jmocki ...

  3. 通过JS,按照原比例控制图片尺寸

    <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...

  4. python3 unittest数据驱动

    我们在自动化测试的时候,有没有遇到这样的问题?例如一个登录的接口要做自动化,会有很多case(用例),密码错误,密码正确这种.在继承unittest.TestCase的类中,凡是以“test”开头的方 ...

  5. hive取等分数据

    %sql select t3.* from ( select t2.* ,row_number() over(partition by t2.pt order by t2.pv) as rn2 fro ...

  6. angularJs(2)表单中下拉框单选多选

    多选 <input type="checkbox" ng-model='game' ng-true-value="1" ng-false-value=&q ...

  7. 12-----BBS论坛

    BBS论坛(十二) 12.1.图形验证码生成 (1)utils/captcha/init.py import random import string # Image:一个画布 # ImageDraw ...

  8. Selenium+Python+Webdriver:保存截图到指定文件夹

    保存图片到指定文件夹: from selenium import webdriverfrom pathlib import Pathfrom time import sleepdriver = web ...

  9. python自学-day2(变量、if条件判断、运算符操作)

    1.变量 变量只是用于保存内存位置,将变量存储在内存中的作用,方便后面调用,这意味着,在创建变量时会在内存中开辟一个空间. 变量命名规则: 由字母.数字.下划线(_)组成 不能以数字开头 不能使用 P ...

  10. RTT之内核服务函数

    一 延时函数: rt_thread_delay(t) //调用时进入系统调度. rt_kprintf()函数在kservice.c中实现,如果不使用设备驱动,则由自定义函数void rt_hw_con ...