Given a string s
, find the length of the longest repeating substring.
If there is no repeating substring, return 0
.
Example 1:
Input: s = "banana"
Output: 3
Explanation: The longest repeating substrings are "ana" and "nan", which appear in the positions [1,3] and [2,4] respectively.
Example 2:
Input: s = "ababc"
Output: 2
Explanation: The longest repeating substring is "ab", which appears in the positions [0,2].
Example 3:
Input: s = "aa"
Output: 1
Constraints:
1 <= s.length <= 2000
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 the longest repeating part of a word is like comparing every possible piece of the word with every other possible piece. We check all possible starting points and lengths to see if there's a repeat.
Here's how the algorithm would work step-by-step:
def longest_repeating_substring_brute_force(input_string):
longest_repeating_substring = ""
string_length = len(input_string)
for substring_length in range(1, string_length):
for first_substring_start in range(string_length - substring_length + 1):
first_substring = input_string[first_substring_start:first_substring_start + substring_length]
# Start searching for repeats after the end of the first substring.
for second_substring_start in range(first_substring_start + 1, string_length - substring_length + 1):
second_substring = input_string[second_substring_start:second_substring_start + substring_length]
# Check if the substrings match.
if first_substring == second_substring:
# Update longest substring if current is longer.
if substring_length > len(longest_repeating_substring):
longest_repeating_substring = first_substring
return longest_repeating_substring
The key is to avoid unnecessary comparisons by intelligently searching for matching substrings. We'll use a suffix tree or suffix array to efficiently organize all possible suffixes of the string. This allows us to quickly find the longest common prefix among the suffixes, which corresponds to the longest repeating substring.
Here's how the algorithm would work step-by-step:
def longest_repeating_substring(input_string):
string_length = len(input_string)
suffixes = []
for i in range(string_length):
suffixes.append(input_string[i:])
suffixes.sort()
longest_substring = ""
# Comparing adjacent suffixes to find the longest common prefix.
for i in range(len(suffixes) - 1):
current_substring = longest_common_prefix(suffixes[i], suffixes[i+1])
if len(current_substring) > len(longest_substring):
longest_substring = current_substring
return longest_substring
def longest_common_prefix(string_one, string_two):
prefix = ""
minimum_length = min(len(string_one), len(string_two))
# Finding common characters between two strings.
for i in range(minimum_length):
if string_one[i] == string_two[i]:
prefix += string_one[i]
else:
break
return prefix
# Creating test cases
input_string = "banana"
# Find the longest repeating substring
result = longest_repeating_substring(input_string)
Case | How to Handle |
---|---|
Empty string input | Return an empty string since no substring can exist. |
String with only one character | Return an empty string since a repeating substring requires at least two characters. |
String with all identical characters (e.g., 'aaaaa') | The longest repeating substring will be the entire string minus one character. |
Very long string input | Ensure the algorithm's time and space complexity is efficient enough to handle large inputs, considering memory constraints. |
String with no repeating substring (e.g., 'abcdefgh') | Return an empty string when no repeating substring is found. |
Overlapping repeating substrings (e.g., 'abababa') | Ensure the algorithm correctly identifies the longest, potentially overlapping, substring. |
Case sensitivity (e.g., 'aB' vs 'ab') | Clarify if the comparison is case-sensitive or case-insensitive and handle accordingly, potentially using toLowerCase() or toUpperCase(). |
String contains special characters or Unicode characters | Ensure the algorithm handles special characters and Unicode characters correctly, considering character encoding. |