Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on string matching fundamentals.
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 <= 104s consists of lowercase English letters.Problem summary: Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: String Matching
"abab"
"aba"
"abcabcabcabc"
find-the-index-of-the-first-occurrence-in-a-string)repeated-string-match)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #459: Repeated Substring Pattern
class Solution {
public boolean repeatedSubstringPattern(String s) {
String str = s + s;
return str.substring(1, str.length() - 1).contains(s);
}
}
// Accepted solution for LeetCode #459: Repeated Substring Pattern
func repeatedSubstringPattern(s string) bool {
return strings.Index(s[1:]+s, s) < len(s)-1
}
# Accepted solution for LeetCode #459: Repeated Substring Pattern
class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return (s + s).index(s, 1) < len(s)
// Accepted solution for LeetCode #459: Repeated Substring Pattern
impl Solution {
pub fn repeated_substring_pattern(s: String) -> bool {
(s.clone() + &s)[1..s.len() * 2 - 1].contains(&s)
}
}
// Accepted solution for LeetCode #459: Repeated Substring Pattern
function repeatedSubstringPattern(s: string): boolean {
return (s + s).slice(1, (s.length << 1) - 1).includes(s);
}
Use this to step through a reusable interview workflow for this problem.
At each of the n starting positions in the text, compare up to m characters with the pattern. If a mismatch occurs, shift by one and restart. Worst case (e.g., searching "aab" in "aaaa...a") checks m characters at nearly every position: O(n × m).
KMP and Z-algorithm preprocess the pattern in O(m) to build a failure/Z-array, then scan the text in O(n) — never backtracking. Total: O(n + m). Rabin-Karp uses rolling hashes for O(n + m) expected time. All beat the O(n × m) brute force of checking every position.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.