*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 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?的更多相关文章
- 43. leetcode 459. Repeated Substring Pattern
459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...
- 459. Repeated Substring Pattern【easy】
459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...
- 459. Repeated Substring Pattern
https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...
- [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 ...
- 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 ...
- 【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 ...
- KMP - LeetCode #459 Repeated Substring Pattern
复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足s ...
- 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 ...
- 459. Repeated Substring Pattern 判断数组是否由重复单元构成
[抄题]: Given a non-empty string check if it can be constructed by taking a substring of it and append ...
随机推荐
- shell入门基础必备
作者:KornLee 2005-02-03 15:32:57 来自:Linux先生 1.建立和运行shell程序 什么是shell程序呢? 简单的说shell程序就是一个包含若干行 shel ...
- IDEA中如何添加jar包
File -> Project Structure -> Libraries -> + -> jar -> 选择classes
- http 和 https 的区别
参考:http://www.cnblogs.com/wqhwe/p/5407468.html HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从W ...
- 分治法 - Divide and Conquer
在计算机科学中,分治法是一种很重要的算法.分治法即『分而治之』,把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题……直到最后子问题可以简单的直接求解,原问题的解即子问题的 ...
- my.ZC
1.100级,裸身,满技能,属性模拟 数据: 大唐 方寸 化生 龙宫 普陀 地府 狮驼 魔王 气血 1200 1900 2600 1200 2600 2600 1900 1900 魔法 7 ...
- python_文件的打开和关闭
文件对象 = open('文件名','使用方式')rt:读取一个txt文件wt: 只写打开一个txt文件,(如果没有该文件则新建该文件)会覆盖原有内容at:打开一个txt文件,并从文件指针位置追加写内 ...
- MapReduce的主要的六个类讲解
a.InputFormat类.该类的作用是将输入的文件和数据分割成许多小的split文件, 并将split的每个行通过LineRecorderReader解析成<Key,Value>,通过 ...
- oracle_expdp_help
[oracle@ctp ~]$ expdp -help Export: Release 11.2.0.3.0 - Production on Thu Feb 28 13:52:15 2019 Copy ...
- 2019.03.20 读书笔记 as is 以及重写隐式/显示
强转.as is 的用法 强制转换类型有两种:子类转基类,重写隐式(implicit )\显示(explicit) 转换操作符 class myclass { private int value; p ...
- LeetCode 303.区域检索-数组不可变(accumulate()和for循环差异分析)
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1],求和函数 ...