String replacement in MATLAB - Time & Space Complexity
We want to understand how the time it takes to replace parts of a string changes as the string gets longer.
How does the work grow when we replace text inside a bigger string?
Analyze the time complexity of the following code snippet.
originalStr = "hello world hello";
searchStr = "hello";
replaceStr = "hi";
newStr = strrep(originalStr, searchStr, replaceStr);
This code replaces all occurrences of "hello" with "hi" in the given string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the original string to find all matches of the search string.
- How many times: The scan goes through the entire string once, checking each position for a match.
As the string gets longer, the function checks more positions to find matches.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the string length.
Time Complexity: O(n*m)
This means the time to replace text grows linearly with the product of the string length and the search string length.
[X] Wrong: "Replacing text takes the same time no matter how long the string is."
[OK] Correct: The function must check each part of the string, so longer strings take more time.
Understanding how string operations grow with input size helps you explain your code choices clearly and confidently.
"What if we replaced multiple different substrings at once? How would the time complexity change?"
Practice
strrep('hello world', 'world', 'MATLAB') do?Solution
Step 1: Understand strrep function
The functionstrrepreplaces all occurrences of a substring with a new substring in a given string.Step 2: Apply strrep to the example
It replaces 'world' with 'MATLAB' in 'hello world', resulting in 'hello MATLAB'.Final Answer:
Replaces 'world' with 'MATLAB' resulting in 'hello MATLAB' -> Option AQuick Check:
strrep('hello world', 'world', 'MATLAB') = 'hello MATLAB' [OK]
- Thinking strrep removes substrings instead of replacing
- Confusing strrep with uppercase functions
- Assuming strrep only works on variables
text = 'cat and cat'?Solution
Step 1: Recall MATLAB string replacement syntax
In MATLAB, the function to replace substrings isstrrep(originalStr, oldSubstr, newSubstr).Step 2: Match the correct syntax
strrep(text, 'cat', 'dog') usesstrrepcorrectly with the right order of arguments.Final Answer:
strrep(text, 'cat', 'dog') -> Option BQuick Check:
Correct MATLAB syntax for string replacement is strrep() [OK]
- Using Python or other language syntax by mistake
- Misspelling function names
- Using dot notation which MATLAB strings don't support
str = 'apple banana apple'; newStr = strrep(str, 'apple', 'orange'); disp(newStr);
Solution
Step 1: Identify the original string and replacement
The original string is 'apple banana apple'. The function replaces all 'apple' with 'orange'.Step 2: Apply strrep to replace all occurrences
Both 'apple' words are replaced, resulting in 'orange banana orange'.Final Answer:
orange banana orange -> Option CQuick Check:
strrep replaces all matches, not just first [OK]
- Replacing only the first occurrence
- Confusing strrep with regex functions
- Expecting original string unchanged
text = 'dog and dog'. What is the error?text = 'dog and dog'; newText = strrep(text, dog, cat); disp(newText);
Solution
Step 1: Check argument types in strrep
In MATLAB, substrings must be given as strings or character arrays enclosed in quotes.Step 2: Identify missing quotes causing error
The code uses dog and cat without quotes, which MATLAB treats as variables, causing an error.Final Answer:
Missing quotes around 'dog' and 'cat' causing an error -> Option AQuick Check:
String literals need quotes in strrep arguments [OK]
- Forgetting quotes around substrings
- Assuming variables are strings automatically
- Thinking disp cannot show strings
words = ["red", "blue", "red", "green"], which MATLAB code replaces all occurrences of "red" with "yellow" correctly?Solution
Step 1: Understand string array replacement functions
For string arrays, MATLAB'sreplacefunction works correctly, whilestrrepworks on character arrays.Step 2: Identify the correct function and syntax
newWords = replace(words, 'red', 'yellow'); usesreplacewith proper quotes, which replaces all "red" with "yellow" in the string array.Final Answer:
newWords = replace(words, 'red', 'yellow'); -> Option DQuick Check:
Use replace() for string arrays, strrep() for char arrays [OK]
- Using strrep on string arrays (causes error)
- Forgetting quotes around substrings
- Confusing variable names with strings
Ready to prove it?
Pass the test and this pattern moves unknown
