Given a string s
, determine if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
For example:
s = "abab"
, return true
because it is the substring "ab" repeated twice.s = "aba"
, return false
.s = "abcabcabcabc"
, return true
because it is the substring "abc" repeated four times or the substring "abcabc" repeated twice.Consider the following constraints:
1 <= s.length <= 10^4
s
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding a repeating substring pattern is like trying every possible piece of the original string and seeing if that piece can be repeated to make the whole thing. We systematically check smaller and smaller chunks to see if they form the core repeating unit.
Here's how the algorithm would work step-by-step:
def repeated_substring_pattern(input_string):
string_length = len(input_string)
# Iterate through possible substring lengths
for substring_length in range(1, string_length):
#Check if substring length is a divisor
if string_length % substring_length == 0:
number_of_repeats = string_length // substring_length
substring = input_string[:substring_length]
# Build the repeated string
repeated_string = substring * number_of_repeats
# Compare repeated string to the input
if repeated_string == input_string:
return True
# No repeating substring found
return False
The core idea is to see if a given string is built by repeating a smaller string multiple times. We'll cleverly use string manipulation to figure out if this repetition exists, avoiding lots of unnecessary checks.
Here's how the algorithm would work step-by-step:
def repeated_substring_pattern(input_string):
doubled_string = input_string + input_string
modified_string = doubled_string[1:-1]
# Removing the first and last characters is crucial
# to avoid trivial matches of the original string
if input_string in modified_string:
# If the original string is found, it's a repeated pattern
return True
else:
return False
Case | How to Handle |
---|---|
Null or Empty String | Return false immediately, as an empty string cannot have a repeated substring pattern. |
String of length 1 | Return false, a single-character string cannot have a repeating substring. |
String with all identical characters | The solution should correctly identify a single character repeated as a valid repeating substring pattern (e.g. 'aaaa'). |
String equal to a single repeating character like 'a' | The code should return false if the string has only one distinct character and is not made of repeating substrings larger than 1 character |
Very long strings exceeding memory constraints | Ensure the solution uses space-efficient techniques, perhaps limiting substring generation or checking length divisors efficiently. |
String composed of a repeating substring exactly twice | Solution should return true if the first half equals the second half. |
String composed of non-repeating characters (e.g., 'abcdefg') | The algorithm should return false, as no repeating substring pattern exists. |
String where the substring repeats more than twice | Verify the solution correctly identifies patterns where the substring repeats multiple times (e.g., 'ababab'). |