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 two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
A shift on s consists of moving the leftmost character of s to the rightmost position.
s = "abcde", then it will be "bcdea" after one shift.Example 1:
Input: s = "abcde", goal = "cdeab" Output: true
Example 2:
Input: s = "abcde", goal = "abced" Output: false
Constraints:
1 <= s.length, goal.length <= 100s and goal consist of lowercase English letters.Problem summary: Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s. A shift on s consists of moving the leftmost character of s to the rightmost position. For example, if s = "abcde", then it will be "bcdea" after one shift.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: String Matching
"abcde" "cdeab"
"abcde" "abced"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #796: Rotate String
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s + s).contains(goal);
}
}
// Accepted solution for LeetCode #796: Rotate String
func rotateString(s string, goal string) bool {
return len(s) == len(goal) && strings.Contains(s+s, goal)
}
# Accepted solution for LeetCode #796: Rotate String
class Solution:
def rotateString(self, s: str, goal: str) -> bool:
return len(s) == len(goal) and goal in s + s
// Accepted solution for LeetCode #796: Rotate String
impl Solution {
pub fn rotate_string(s: String, goal: String) -> bool {
s.len() == goal.len() && (s.clone() + &s).contains(&goal)
}
}
// Accepted solution for LeetCode #796: Rotate String
function rotateString(s: string, goal: string): boolean {
return s.length === goal.length && (goal + goal).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.