Start from the beginning of the text, compare the substring at each position, move forward if no match, stop when match found or text ends.
Execution Sample
DSA Rust
let text = "hello world";
let pattern = "lo";
for i in0..=text.len() - pattern.len() {
if &text[i..i+pattern.len()] == pattern {
println!("Found at {}", i);
break;
}
}
This code searches for the substring "lo" in "hello world" by checking each possible position.
Execution Table
Step
Operation
Index i
Text Segment Compared
Match?
Action
Visual State
1
Compare substring with text segment
0
"he"
No
Move to next index
Text: h e l l o w o r l d
Index: 0 1 2 3 4 5 6 7 8 9 10
2
Compare substring with text segment
1
"el"
No
Move to next index
Text: h e l l o w o r l d
Index: 0 1 2 3 4 5 6 7 8 9 10
3
Compare substring with text segment
2
"ll"
No
Move to next index
Text: h e l l o w o r l d
Index: 0 1 2 3 4 5 6 7 8 9 10
4
Compare substring with text segment
3
"lo"
Yes
Return match index 3
Text: h e l l o w o r l d
Index: 0 1 2 3 4 5 6 7 8 9 10
5
Stop
-
-
-
Match found, stop searching
Text: h e l l o w o r l d
Index: 0 1 2 3 4 5 6 7 8 9 10
💡 Match found at index 3, search stops.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
After Step 4
Final
i
0
1
2
3
3
3
Text Segment Compared
"he"
"el"
"ll"
"lo"
"lo"
"lo"
Match?
No
No
No
Yes
Yes
Yes
Key Moments - 3 Insights
Why do we stop checking after index 3 in this example?
Because the substring length is 2, and the last possible start index is text length - substring length = 11 - 2 = 9. But since we found a match at index 3 (see execution_table step 4), we stop searching early.
Why do we compare text segments of length equal to the substring?
To check if the substring matches exactly at that position, we compare a segment of the text with the same length as the substring (see execution_table column 'Text Segment Compared').
What happens if the substring is not found in the text?
The loop continues until the last possible index, and if no match is found, the search ends without returning an index (not shown in this example but implied by exit conditions).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'i' when the substring matches?
A3
B2
C4
D0
💡 Hint
Check the 'Index i' column at the step where 'Match?' is 'Yes' (step 4).
At which step does the search stop according to the execution_table?
AStep 3
BStep 5
CStep 4
DStep 2
💡 Hint
Look for the step with action 'Match found, stop searching' in the 'Action' column.
If the substring was "wo" instead of "lo", at which index would the match occur?
A6
B3
C4
D7
💡 Hint
Visualize the text and find where "wo" appears; check the 'Index i' positions in the variable_tracker.
Concept Snapshot
Substring Search Patterns:
- Start from index 0 in text
- Compare substring length segment at each index
- If match, return index
- Else, move to next index
- Stop when match found or end reached
Full Transcript
This visualization shows how substring search works by checking each possible position in the text for a match with the substring. We start at index 0 and compare the substring length segment of the text. If it matches, we return the index and stop. If not, we move to the next index and repeat until we find a match or reach the end of the text. The example searches for "lo" in "hello world" and finds it at index 3.
Practice
(1/5)
1. What is the main purpose of substring search algorithms in strings?
easy
A. To find if a smaller string exists inside a larger string
B. To sort characters in a string alphabetically
C. To reverse the order of characters in a string
D. To count the total number of characters in a string
Solution
Step 1: Understand substring search goal
Substring search algorithms check if a smaller string (pattern) appears inside a bigger string (text).
Step 2: Compare with other string operations
Sorting, reversing, and counting characters are different operations unrelated to searching for a substring.
Final Answer:
To find if a smaller string exists inside a larger string -> Option A