Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Use `s.contains(sub)` / `sub in s`. To show the algorithm, check each start index `i` in `0..n-m` and compare the m characters. O(n·m) naive.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can write a correct windowed match and know its complexity / the KMP upgrade.
Where candidates slip
Start-range off-by-one, or mishandling the empty needle.
How to stand out
Describe KMP's failure function for O(n+m) matching.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Sliding window compare
1Hint 1Explore hintHide hint
Try each start position where sub could still fit in s.
2Hint 2Explore hintHide hint
Compare |sub| characters at each start; what's the worst-case cost?
3Hint 3Explore hintHide hint
Remember the empty-needle case, and name the O(n+m) KMP upgrade.
A · Core idea
Decide whether sub occurs inside s. The library way is s.contains(sub); the algorithmic way is a sliding-window compare: at each start index in s, check whether the next |sub| characters match.
Why O(n·m) naive. There are up to n−m+1 start positions, and each comparison can take up to m characters, so the brute force is O(n·m). It's fine for fresher scope, but name the upgrade.
The empty-needle convention. An empty sub is a substring of every string (it 'occurs' at position 0), so return true for it. Mishandling this is a common edge-case slip.
The linear upgrade. KMP precomputes a failure function so it never re-compares characters, achieving O(n+m); indexOf/contains are typically optimised similarly. Mentioning KMP shows you know the brute force isn't optimal.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
s = "abcab", sub = "cab"i=0
abc vs cab
notrueC · Code with teaching comments
D · Why it works and complexityO(n·m) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
Mention KMP/indexOf as the O(n+m) upgrade over the O(n·m) scan.
ComplexityO(n·m) time, O(1) space — up to n−m+1 starts, each up to m comparisons → O(n·m); O(1) extra space.
E · Edge cases and pitfalls5s = "abc", sub = "" — Empty needle → true.Explore edge casesHide edge cases
Edge cases handled
s = "abc", sub = ""Empty needle → true.s = "abc", sub = "d"Absent → false.s = "abc", sub = "abc"Whole string matches → true.
Pitfalls
- Off-by-one on the start range (i + m <= n).
- Not returning true for an empty needle.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic boolean contains(String s, String sub) { int n = s.length(), m = sub.length(); for (int i = 0; i + m <= n; i++) { int j = 0; while (j < m && s.charAt(i + j) == sub.charAt(j)) j++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Off-by-one on the start range (i + m <= n).
- Not returning true for an empty needle.
Revise
Carry the pattern into the interview
Pattern to remember
Sliding window match
Code pattern to memorize
for i in 0..n-m: if s[i:i+m]==sub return true; (empty -> true)Rules to remember
- 1Try each start 0..n-m.
- 2Compare m chars.
- 3Empty needle ⟹ true.
Use this pattern when
- Substring search
Choose another pattern when
- Very large inputs — use KMP/Z-algorithm for O(n+m)
Interview takeaway
Substring search is window matching; KMP makes it linear.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Sliding window match. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Implement strStr / indexOfComing soonReturn the index; KMP for O(n+m).