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. 1065. A+B and C (64bit) (20)

    Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Input S ...

  2. spark 2.X 疑难问题汇总

    当前spark任务都是运行在yarn上,所以不用启动长进程worker,也没有master的HA问题,所以主要的问题在任务执行层面. 作业故障分类故障主要分为版本,内存和权限三方面. - 各种版本不一 ...

  3. SQL Server 数据导入与导出

    1. BCP 命令 用法: bcp {dbtable | query} {in | out | queryout | format} 数据文件 [-m 最大错误数] [-f 格式化文件] [-e 错误 ...

  4. Python 错误总结

    1.以一种访问权限不允许的方式做了一个访问套接字的尝试. 解决方法:这个问题缘由是有端口被占用

  5. js遍历table和gridview

    //遍历table var tableObj = document.getElementById("tableName");var str = "";for(v ...

  6. 微信小程序多图上传/朋友圈传图效果【附完整源码】

    效果图 部分源代码 js文件: var uploadPicture = require('../Frameworks/common.js') //获取应用实例 const app = getApp() ...

  7. Spring实现AOP

    转载: https://blog.csdn.net/tolcf/article/details/49133119 [框架][Spring]XML配置实现AOP拦截-切点:JdkRegexpMethod ...

  8. 查看CPU和内存,用机器指令和汇编指令编程【Debug模式】

    命令 作用 举例 R 查看,改变CPU寄存器的内容 查看:r 改写:r ax D 查看内存中的内容 d 1000:0 f E 改写内存中的内容 e 1000:0 f U 将内存中的机器指令翻译成汇编指 ...

  9. Mybatis学习笔记5 - 参数处理

    1.单个参数:mybatis不会做特殊处理,#{参数名}:取出参数值. 2.多个参数:mybatis会做特殊处理. 多个参数会被封装成 一个map, key:param1...paramN,或者参数的 ...

  10. android Activity启动过程(四)startActivityUncheckedLocked

    final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteract ...