Skip to content

Commit 9e624ce

Browse files
committedDec 10, 2020
solves repeated substring pattern
1 parent 5b5d079 commit 9e624ce

5 files changed

+45
-3
lines changed
 

Diff for: ‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
| 448 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FindAllNumbersDisappearedInAnArray.java) |
121121
| 453 | [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-moves-to-equal-array-elements) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MinimumMovesToEqualArrayElements.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/minimum_moves_to_equal_array_element.py) |
122122
| 455 | [Assign Cookies](https://leetcode.com/problems/assign-cookies) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/AssignCookies.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/assign_cookies.py)|
123-
| 459 | [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern) | Easy | |
123+
| 459 | [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RepeatedSubstringPattern.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/repeated_substring_pattern.py) |
124124
| 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance) | Easy | |
125125
| 463 | [Island Perimeter](https://leetcode.com/problems/island-perimeter) | Easy | |
126126
| 475 | [Heaters](https://leetcode.com/problems/heaters) | Easy | |

Diff for: ‎python/repeated_substring_pattern.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def repeatedSubstringPattern(self, pattern: str) -> bool:
3+
for length in range(1, len(pattern) // 2 + 1):
4+
if len(pattern) % length == 0:
5+
substring = pattern[:length]
6+
if self.patternComposedOf(pattern, substring):
7+
return True
8+
return False
9+
10+
def patternComposedOf(self, pattern: str, substring: str) -> bool:
11+
for i in range(len(pattern) // len(substring)):
12+
for j in range(len(substring)):
13+
if substring[j] != pattern[i * len(substring) + j]:
14+
return False
15+
return True
16+
17+
18+
sol = Solution()
19+
print(sol.repeatedSubstringPattern('a'))

Diff for: ‎src/IntersectionOfTwoArrays.java

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.List;
44
import java.util.Set;
55

6-
IntersectionOfTwoArrays
76
public class IntersectionOfTwoArrays {
87
public static int[] intersection(int[] array1, int[] array2) {
98
Set<Integer> numbers1 = setFrom(array1);

Diff for: ‎src/RepeatedSubstringPattern.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class RepeatedSubstringPattern {
2+
public static boolean repeatedSubstringPattern(String pattern) {
3+
for (int len = 1 ; len <= pattern.length() / 2 ; len++) {
4+
if (pattern.length() % len == 0) {
5+
String substring = pattern.substring(0, len);
6+
if (patternComposedOf(pattern, substring)) {
7+
return true;
8+
}
9+
}
10+
}
11+
return false;
12+
}
13+
14+
private static boolean patternComposedOf(String pattern, String subString) {
15+
for (int i = 0 ; i < pattern.length() / subString.length() ; i++) {
16+
for (int j = 0 ; j < subString.length() ; j++) {
17+
if (subString.charAt(j) != pattern.charAt(i * subString.length() + j)) {
18+
return false;
19+
}
20+
}
21+
}
22+
return true;
23+
}
24+
}

Diff for: ‎src/ReverseVowelsOfString.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static String reverseVowels(String string) {
2424
StringBuilder result = new StringBuilder();
2525
for (int index = 0, j = vowels.size() - 1 ; index < string.length() ; index++) {
2626
char character = string.charAt(index);
27-
result.append(isVowel(character) ? vowels.get(j--) : character);
27+
result.append((char) (isVowel(character) ? vowels.get(j--) : character));
2828
}
2929
return result.toString();
3030
}

0 commit comments

Comments
 (0)
Please sign in to comment.