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. centos7安装与卸载软件

    安装 yum install 服务名 查看服务名 rpm -qa |grep -i aerospike 或者 yum list installed | grep aerospike 卸载 yum re ...

  2. spring aop实现log 日志跟踪

    之前写的那篇是基于springboot的(https://www.cnblogs.com/yaoyuan2/p/10302802.html),由于遗留项目用的是spring,因此需要在spring基础 ...

  3. grep常用选项记录

    grep: 一.常用选项:    -i 不区分大小写针对单个字符    -v 显示不包括查找字符的所有行    -o 只打印出匹配到的字符    -c 显示有多少行被匹配到    -e 可以使用多个表 ...

  4. math.random()方法的使用

    一:导言 以前总是被数字的范围正则搞的头大,在此总结了一下 二:用法 Math.random()函数返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 生成n-m,包含n但不包含m的整数: ...

  5. python 安装 第三方包

    ########1 (python 虚拟环境(如pycharm 中的 project )是一个独立的环境,所以也要重新安装一次第三方包) 上官网搜索 包 https://pypi.org/projec ...

  6. 配置编译器(GCC和GFortran)

    平台信息 Description: CentOS Linux release 7.6.1810 (Core) 检查环境 $ gfortran -v $ gcc -v 安装 GCC和Fortran 环境 ...

  7. 配置WAMP完美攻略

    软件介绍 Wamp Server 是一款功能强大的PHP 集成安装环境. 为了节约时间,本次使用 Wamp Server 来进行配置. wamp 的全部含义就是本篇文章的标题. 使用版本和操作系统 W ...

  8. Django-4 模板层

    你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now = datet ...

  9. Android官方架构组件介绍之应用(四)

    讲一个项目常见的功能,友盟统计功能 例如一个项目有很多多modlue,每个里面modlue都有Activity,Activity需要友盟统一,Fragment也需要友盟统计.一般做法就是继承一个Bas ...

  10. POI 按word模版生成合同并生成PDF

    功能需求:根据用户给的word版本合同文件.docx,实现模版替换功能. 如: 功能:支持段落和表格里的文字替换,还可以支持表格替换.如果需要段落需要换行用<br>隔开如:身份证<b ...