Find the bug · Easy
Fix the Substring Check
Easy Find the bug Most asked
containsWord(text, word) returns true when word occurs anywhere in text (case-sensitive), and false otherwise.
A user reports that searching for "Error" in "Error: disk full" returns false. Find the bug and fix it. The same bug exists in every language version.
Example 1
Input: text = "disk full: Error", word = "Error" Output: true
Example 2
Input: text = "Error: disk full", word = "Error" Output: true Explanation: The buggy version returns false.
The buggy code
function containsWord(text, word) {
if (text.indexOf(word)) return true;
return false;
}
function containsWord(text: string, word: string): boolean {
if (text.indexOf(word)) return true;
return false;
}
def contains_word(text: str, word: str) -> bool:
if text.find(word):
return True
return False
<?php
function containsWord(string $text, string $word): bool
{
if (strpos($text, $word)) {
return true;
}
return false;
}
class Solution {
public boolean containsWord(String text, String word) {
if (text.indexOf(word) > 0) return true;
return false;
}
}
Hints
Hint 1
What does indexOf / strpos / find return when the match starts at position 0?
Hint 2
And what do they return when there is no match at all?
Find and fix the bug
Loading the editor… (JavaScript is required to run code in your browser.)
Solution and explanation
Show the bug and the fix
Time O(n x m) · Space O(1)
The bug: the position of the match is used as a boolean. A match at position 0 is falsy, so it reads as "not found". In JavaScript and Python the no-match value (-1) is truthy, so the check is also backwards for missing words; in PHP, strpos returns false for no match and 0 for a match at the start, and both look false to if. Java's version compares > 0, which has the same off-by-one.
The fix: use the API that returns a boolean (includes, in, str_contains, contains), or compare explicitly against the not-found value (!== -1, !== false, >= 0).
This is one of the most common PHP interview gotchas, and the PHP manual has a warning box about it on the strpos page.
Fixed version
function containsWord(text, word) {
return text.includes(word); // or: text.indexOf(word) !== -1
}
function containsWord(text: string, word: string): boolean {
return text.includes(word); // or: text.indexOf(word) !== -1
}
def contains_word(text: str, word: str) -> bool:
return word in text # or: text.find(word) != -1
<?php
function containsWord(string $text, string $word): bool
{
return str_contains($text, $word); // PHP 8; before: strpos($text, $word) !== false
}
class Solution {
public boolean containsWord(String text, String word) {
return text.contains(word); // or: text.indexOf(word) >= 0
}
}
Common mistakes
- PHP:
if (strpos(...) != false)with loose comparison: 0 == false is true, so it is still wrong.
More Code Clinic: Find the Bug problems
- Fix the Binary Search · Easy
- Fix the Two Sum Hash Map · Easy
- Fix the Longest Substring Without Repeats · Medium
- Fix Merge Intervals · Medium
- Fix the Mutable Default Argument · Easy
- Fix the Median (JavaScript Sort) · Easy