-
Notifications
You must be signed in to change notification settings - Fork 820
/
RepeatedSubstringPattern.java
53 lines (51 loc) · 1.42 KB
/
RepeatedSubstringPattern.java
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
43
44
45
46
47
48
49
50
51
52
53
/* (C) 2024 YourCompanyName */
package string;
/**
* Created by gouthamvidyapradhan on 25/03/2017. 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.
*
* <p>Example 1: Input: "abab"
*
* <p>Output: True
*
* <p>Explanation: It's the substring "ab" twice. Example 2: Input: "aba"
*
* <p>Output: False Example 3: Input: "abcabcabcabc"
*
* <p>Output: True
*
* <p>Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
*/
public class RepeatedSubstringPattern {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(new RepeatedSubstringPattern().repeatedSubstringPattern("a"));
}
public boolean repeatedSubstringPattern(String s) {
boolean found;
for (int i = 1, l = s.length(); i < l; i++) {
found = true;
String subI = s.substring(0, i);
int j = i;
if (j >= l) return false;
while (j < l) {
if ((j + i) >= l + 1) return false;
String subJ = s.substring(j, j + i);
if (!subI.equals(subJ)) {
found = false;
break;
}
j += i;
}
if (found) return true;
}
return false;
}
}