ID | Title | Difficulty | |
---|---|---|---|
Loading... |
459. Repeated Substring Pattern
Easy
LeetCode
String, String Matching
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 1:
Input: s = "abab"
Output: true
Explanation: It is the substring "ab" twice.
Example 2:
Input: s = "aba"
Output: false
Example 3:
Input: s = "abcabcabcabc"
Output: true
Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
Code
class Solution {
public boolean repeatedSubstringPattern(String s) {
int len = s.length();
for(int i = len / 2; i >= 1; i--) {
if(len % i == 0) {
int groupNum = len / i;
String sub = s.substring(0, i);
StringBuilder sb = new StringBuilder();
for(int j = 0; j < groupNum; j++) {
sb.append(sub);
}
if(sb.toString().equals(s)) return true;
}
}
return false;
}
}
按 <- 键看上一题!
457. Circular Array Loop
按 -> 键看下一题!
461. Hamming Distance