-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write LeetCode Post " LeetCode : 459. Repeated Substring Pattern "
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
layout: post | ||
title: " LeetCode : 459. Repeated Substring Pattern " | ||
categories: LeetCode | ||
author: goodGid | ||
use_math: true | ||
--- | ||
* content | ||
{:toc} | ||
|
||
## [459. Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern) | ||
|
||
### Problem | ||
|
||
``` | ||
Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. | ||
``` | ||
|
||
|
||
--- | ||
|
||
### Example | ||
|
||
``` | ||
Input: s = "abab" | ||
Output: true | ||
Explanation: It is the substring "ab" twice. | ||
``` | ||
|
||
--- | ||
|
||
### [1] Code (23. 10. 08) | ||
|
||
*Retry* | ||
|
||
``` java | ||
// Runtime: 42 ms | ||
// Memory Usage: 44.2 MB | ||
// Ref : https://leetcode.com/submissions/detail/1067270115 | ||
class Solution { | ||
public boolean repeatedSubstringPattern(String s) { | ||
int n = s.length(); | ||
|
||
for (int i = 1; i <= n/2; i++) { | ||
String key = s.substring(0, i); | ||
// System.out.println("key : " + key); | ||
if (check(n, s, i, key)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean check(int n, String s, int i, String key) { | ||
if (n % i != 0) { | ||
return false; | ||
} | ||
|
||
for (int idx = i; idx + i <= n; ) { | ||
String target = s.substring(idx, idx + i); | ||
// System.out.println("target : " + target); | ||
idx += i; | ||
if (!key.equals(target)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
``` | ||
|
||
* 깔끔한 구현 능력을 필요로 했던 문제 | ||
|
||
--- | ||
|
||
## Reference | ||
|
||
* [459. Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern) |