*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 ...
随机推荐
- lintcode - 恢复ip地址
class Solution { public: /* * @param s: the IP string * @return: All possible valid IP addresses */ ...
- Unix shell判断和比较
1. shell 的$! ,$?, $$,$@ $n $1 the first parameter,$2 the second... $# The number of c ...
- 第四章 Appium真机运行测试用例讲解
-----手机自动化之Appium 手机自动化测试用例虽然可以在模拟器上运行,可是模拟器毕竟和真机还是有区别的.在第二章我们讲到了模拟器上运行测试用例后,我又花了两天的时间,研究了一下真机运行测试用例 ...
- Java 字节流和字符流
程序中都是以流的形式进行数据的传输和保存,在java.io包中数据流操作的两大类是字节流和字符流. 1. 字节流 InputStream和OutputStream是所有表示字节流的类的父类,它们都是抽 ...
- Nuxt 2.3.X 配置babel
1. 在package.json中修改运行脚本 添加--exec babel-node 添加之后的效果为:(修改了8/10行) { "name": "nuxt-learn ...
- IOS Intro - NSDictionary and NSMutableDictionary
NSDictionary.NSMutableDictionary的基本用法 1.不可变词典NSDictionary 字典初始化 NSNumber *numObj = [NSNumber numberW ...
- Unity String 转换成 Vector3
- (转)iptables简介
iptables简介 原文:https://www.cnblogs.com/metoy/p/4320813.html netfilter/iptables(简称为iptables)组成Linux平台下 ...
- 020-pom.xml配置文件模板
1 Maven 整合SSH框架之pom.xml 1 版本一 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns: ...
- XStream xml转java对象2
<?xml version="1.0" encoding="UTF-8" ?> <person> <name>yunyun& ...