All topics
126

Finding a word inside a bigger text

Tier 1Locked

Where it runs

1. The problem

Where does "world" start in "hello world"? How many times does "ab" show up in "ababab"? These questions are behind every Ctrl-F, every grep, every "does this log line contain ERROR". Languages give you ready-made indexOf, strstr or in for this. Here you write the search yourself. The plain version teaches you to compare characters while sliding the pattern along the text, and many string problems reuse that move later. And once you know what the plain version costs, you can tell when you need a faster algorithm.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included

On LeetCode

Problems that find a short pattern inside a text, or check whether one string sits inside another. Trying every start and comparing letter by letter costs O(n * m), which passes when the strings are short.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy