Created
September 19, 2017 00:14
-
-
Save cixuuz/602cd9514fcb2acbe9f0d73619908a40 to your computer and use it in GitHub Desktop.
[459. Repeated Substring Pattern] #leetcode
This file contains hidden or 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
class Solution { | |
// O(n^3) O(n) | |
public boolean repeatedSubstringPattern(String s) { | |
int n = s.length(); | |
for (int i=1; i<=n/2; i++) { | |
if (n%i == 0) { | |
StringBuilder sb = new StringBuilder(); | |
for (int j=0; j<n/i; j++) { | |
sb.append(s.substring(0, i)); | |
} | |
if (s.equals(sb.toString())) return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment