String searching (contains, strfind) in MATLAB - Time & Space Complexity
We want to understand how the time it takes to search for a smaller string inside a bigger string changes as the strings get longer.
How does the search time grow when the input strings grow?
Analyze the time complexity of the following code snippet.
text = 'hello world, welcome to matlab';
pattern = 'come';
index = strfind(text, pattern);
found = contains(text, pattern);
This code looks for the smaller string pattern inside the bigger string text using two functions: strfind and contains.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each position in the big string to see if the smaller string starts there.
- How many times: For each character in the big string, it compares up to the length of the smaller string.
As the big string gets longer, the number of checks grows roughly by the length of the big string times the length of the smaller string.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times length of pattern |
| 100 | About 100 times length of pattern |
| 1000 | About 1000 times length of pattern |
Pattern observation: The work grows roughly in a straight line with the big string size, multiplied by the smaller string size.
Time Complexity: O(n*m)
This means the time to search grows proportionally to the length of the big string times the length of the smaller string.
[X] Wrong: "Searching for a string inside another always takes the same time no matter how long the strings are."
[OK] Correct: The search checks many positions and compares characters, so longer strings mean more work and more time.
Understanding how string search time grows helps you explain and reason about code efficiency clearly, a useful skill in many programming tasks.
"What if the smaller string is always just one character? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of
containscontainsreturns true or false if a substring is found inside a string.Step 2: Compare with
strfindstrfindreturns positions, not logical true/false.Final Answer:
contains-> Option AQuick Check:
Logical check for substring = contains [OK]
- Confusing strfind with contains
- Thinking strfind returns true/false
- Using findstr which is outdated
str = 'concatenate' using strfind?Solution
Step 1: Recall
The first argument is the main string, second is the substring to find.strfindsyntaxStep 2: Check each option
strfind(str, 'cat') correctly usesstrfind(str, 'cat'). strfind('cat', str) reverses arguments. contains(str, 'cat') uses contains, not strfind. strfind(str, cat) misses quotes around 'cat'.Final Answer:
strfind(str, 'cat') -> Option CQuick Check:
strfind(mainString, substring) = strfind(str, 'cat') [OK]
- Swapping arguments in strfind
- Forgetting quotes around substring
- Using contains instead of strfind for indices
str = 'abracadabra';
pos = strfind(str, 'ra');
disp(pos);
Solution
Step 1: Identify substring positions in 'abracadabra'
Look for 'ra' in the string: 'abracadabra'. The substring 'ra' starts at index 3 and 10.Step 2: Confirm output of strfind
strfindreturns starting indices of matches, so output is [3 10].Final Answer:
[3 10] -> Option AQuick Check:
Positions of 'ra' = [3 10] [OK]
- Counting indices starting at 0 instead of 1
- Confusing substring length with position
- Expecting logical output instead of indices
str = 'hello world';
pos = strfind('world', str);
disp(pos);
Solution
Step 1: Check strfind argument order
strfindexpects the main string first, then the substring. Here, arguments are reversed.Step 2: Understand impact of reversed arguments
Searching 'world' inside 'hello world' is correct, but reversed arguments cause wrong or empty output.Final Answer:
Arguments of strfind are reversed -> Option BQuick Check:
strfind(mainString, substring) correct order [OK]
- Swapping arguments in strfind
- Assuming disp can't show arrays
- Thinking semicolon is mandatory for correctness
str = 'banana bandana' and check if it exists. Which code correctly does this?Solution
Step 1: Use
strfindto get positionsstrfind(str, 'ana')returns all starting indices of 'ana' in the string.Step 2: Use
containsto check existencecontains(str, 'ana')returns true if 'ana' is found anywhere instr.Step 3: Verify options
positions = strfind(str, 'ana'); exists = contains(str, 'ana'); correctly uses both functions. positions = contains(str, 'ana'); exists = strfind(str, 'ana'); swaps roles. positions = strfind('ana', str); exists = contains('ana', str); reverses arguments. positions = find(str, 'ana'); exists = contains(str, 'ana'); uses invalid functionfindfor strings.Final Answer:
positions = strfind(str, 'ana'); exists = contains(str, 'ana'); -> Option DQuick Check:
Positions with strfind, existence with contains [OK]
- Swapping arguments in strfind or contains
- Using find instead of strfind
- Confusing output types of contains and strfind
Ready to prove it?
Pass the test and this pattern moves unknown
