Start comparing the substring at each index of the text. If it matches, record the position. Move forward until the text end.
Execution Sample
DSA Javascript
const text = 'hellohello';
const pattern = 'llo';
for(let i = 0; i <= text.length - pattern.length; i++) {
if(text.substring(i, i + pattern.length) === pattern) {
console.log(i);
}
}
Search for the substring 'llo' inside 'hellohello' and print all starting indices where it matches.
Execution Table
Step
Index i
Text Segment Compared
Match?
Action
Output Indices Found So Far
1
0
'hel'
No
Move to next index
[]
2
1
'ell'
No
Move to next index
[]
3
2
'llo'
Yes
Record index 2
[2]
4
3
'loh'
No
Move to next index
[2]
5
4
'ohe'
No
Move to next index
[2]
6
5
'hel'
No
Move to next index
[2]
7
6
'ell'
No
Move to next index
[2]
8
7
'llo'
Yes
Record index 7
[2,7]
9
8
'lo'
No
Move to next index
[2,7]
10
9
'o'
No
Move to next index
[2,7]
11
10
End of search
-
Stop
[2,7]
💡 Index 10 exceeds text length - pattern length, so search ends.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
After Step 4
After Step 5
After Step 6
After Step 7
After Step 8
After Step 9
After Step 10
Final
i
0
1
2
3
4
5
6
7
8
9
10
10
Output Indices
[]
[]
[2]
[2]
[2]
[2]
[2]
[2,7]
[2,7]
[2,7]
[2,7]
[2,7]
Key Moments - 3 Insights
Why do we stop the loop at text.length - pattern.length?
Because beyond that index, the remaining text is shorter than the pattern, so no full match is possible. See execution_table step 11 where index 10 ends the search.
Why do we compare text.substring(i, i + pattern.length) instead of just one character?
Because we need to check the whole pattern length at each position to find a full match, not just a single character. Execution_table shows comparing 3 characters each time.
What happens if the pattern is empty?
If pattern length is zero, substring comparisons always match empty string, so matches would be found at every index. This is a special case not shown here.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'i' when the first match is found?
A2
B0
C7
D8
💡 Hint
Check the 'Index i' column where 'Match?' is 'Yes' for the first time.
At which step does the search stop according to the execution_table?
AStep 7
BStep 11
CStep 8
DStep 10
💡 Hint
Look for the row where 'Action' is 'Stop' and 'Index i' indicates end of search.
If the pattern was 'hel' instead of 'llo', which index would be recorded first?
A7
B2
C0
DNo match
💡 Hint
Consider the substring at index 0 in the text and compare with 'hel'.
Concept Snapshot
Substring Search Pattern:
- Loop from i=0 to text.length - pattern.length
- At each i, compare text substring of pattern length
- If match, record index i
- Stop when no more full pattern fits
- Prints all start indices where pattern occurs
Full Transcript
This visual trace shows how substring search works by sliding over the text and comparing segments equal to the pattern length. At each step, the substring from the current index is compared to the pattern. If they match, the index is recorded. The loop stops when the remaining text is shorter than the pattern. The example searches for 'llo' in 'hellohello' and finds matches at indices 2 and 7.
Practice
(1/5)
1. What does the indexOf method do when used on a string in JavaScript?
easy
A. Converts the string to uppercase
B. Replaces a substring with another string
C. Splits the string into an array of substrings
D. Finds the position of the first occurrence of a substring
Solution
Step 1: Understand the purpose of indexOf
The indexOf method searches a string for a specified substring and returns the position of its first occurrence.
Step 2: Confirm behavior on strings
If the substring is found, it returns the zero-based index; if not found, it returns -1.
Final Answer:
Finds the position of the first occurrence of a substring -> Option D
Quick Check:
indexOf = position of substring [OK]
Hint: Remember: indexOf returns position or -1 if not found [OK]
Common Mistakes:
Confusing indexOf with replace method
Thinking indexOf splits the string
Assuming indexOf changes the string case
2. Which of the following is the correct syntax to check if the substring 'cat' exists in the string text using includes method?
easy
A. text.include('cat')
B. includes(text, 'cat')
C. text.includes('cat')
D. text.contains('cat')
Solution
Step 1: Recall the correct method name
The correct method to check substring presence is includes, called on the string object.
Step 2: Verify syntax
The syntax is string.includes(substring), so text.includes('cat') is correct.