Repeated Substring Pattern
EASYDescription
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.
Constraints:
1 <= s.length <= 104sconsists of lowercase English letters.
Approaches
Checkout 4 different approaches to solve Repeated Substring Pattern. Click on different approaches to view the approach and algorithm in detail.
Brute Force Iteration
This approach tests every possible length for the repeating substring. A valid substring must have a length l that is a divisor of the total string length n. We can iterate through all possible lengths l from 1 up to n/2. For each l, we check if the entire string s is formed by repeating the prefix of length l.
Algorithm
- Get the length of the string,
n. - Iterate
lfrom 1 ton / 2. - If
nis divisible byl: a. Extract the substringsub = s.substring(0, l). b. Assume the pattern holds (match = true). c. Iteratejfromlton - lwith a step ofl. d. Ifs.substring(j, j + l)is not equal tosub, setmatch = falseand break the inner loop. e. Ifmatchis still true after the inner loop, returntrue. - If the outer loop finishes, return
false.
The algorithm iterates through possible substring lengths l from 1 to s.length() / 2. For a length l to be a candidate, we first check if the total length n is divisible by l. If it is, we extract the first substring of length l, let's call it sub. We then verify if the rest of the string s consists of repetitions of sub. This is done by comparing sub with every subsequent block of l characters in s. If all blocks match sub, we have found a valid pattern and return true. If we find a mismatch, we move on to the next possible length. If the loop completes without finding any such pattern, we return false.
class Solution {
public boolean repeatedSubstringPattern(String s) {
int n = s.length();
for (int l = 1; l <= n / 2; l++) {
if (n % l == 0) {
String sub = s.substring(0, l);
boolean match = true;
for (int j = l; j < n; j += l) {
if (!s.substring(j, j + l).equals(sub)) {
match = false;
break;
}
}
if (match) {
return true;
}
}
}
return false;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- A straightforward, direct translation of the problem statement.
- Inefficient for long strings, as it checks many unnecessary lengths.
- Likely to result in a 'Time Limit Exceeded' error on competitive programming platforms for larger inputs.
Code Solutions
Checking out 3 solutions in different languages for Repeated Substring Pattern. Click on different languages to view the code.
class Solution {
public
boolean repeatedSubstringPattern(String s) {
String str = s + s;
return str.substring(1, str.length() - 1).contains(s);
}
}
Video Solution
Watch the video walkthrough for Repeated Substring Pattern
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.