-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRepeatedSubstringPattern.java
More file actions
42 lines (34 loc) · 1.47 KB
/
RepeatedSubstringPattern.java
File metadata and controls
42 lines (34 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// 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.
// See: https://leetcode.com/problems/repeated-substring-pattern/
package leetcode.string;
public class RepeatedSubstringPattern {
public boolean repeatedSubstringPattern(String s) {
for (int len = 1; len <= s.length()/2; len++)
if (s.length() % len == 0) {
for (int i = 0; i < s.length() - len; i++) {
if (s.charAt(i) != s.charAt(i + len)) break;
if (i == s.length() - len - 1)
return true;
}
}
return false;
}
/**
* Elementary solution.
*/
public boolean repeatedSubstringPattern_var1(String s) {
for (int len = 1; len <= s.length()/2; len++)
if (s.length() % len == 0)
if (s.replace(s.substring(0, len), "").isEmpty())
return true;
return false;
}
public static void main(String[] args) {
RepeatedSubstringPattern sln = new RepeatedSubstringPattern();
System.out.println(sln.repeatedSubstringPattern("abab"));
System.out.println(sln.repeatedSubstringPattern("aba"));
System.out.println(sln.repeatedSubstringPattern("abcabcabcabc"));
}
}