String comparison in MATLAB - Time & Space Complexity
When we compare two strings in MATLAB, the time it takes depends on how long the strings are.
We want to know how the work grows as the strings get longer.
Analyze the time complexity of the following code snippet.
str1 = 'hello world';
str2 = 'hello there';
isEqual = true;
for i = 1:length(str1)
if str1(i) ~= str2(i)
isEqual = false;
break;
end
end
This code compares two strings character by character until it finds a difference or reaches the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop comparing each character of the two strings.
- How many times: Up to the length of the shorter string (n times).
As the strings get longer, the number of comparisons grows roughly the same as the string length.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 character comparisons |
| 100 | Up to 100 character comparisons |
| 1000 | Up to 1000 character comparisons |
Pattern observation: The work grows directly with the length of the strings.
Time Complexity: O(n)
This means the time to compare grows in a straight line as the strings get longer.
[X] Wrong: "Comparing two strings always takes the same time regardless of length."
[OK] Correct: Actually, the program checks each character until it finds a difference or finishes, so longer strings usually take more time.
Understanding how string comparison time grows helps you explain efficiency clearly and shows you know how basic operations scale with input size.
"What if the strings are always very different at the first character? How would the time complexity change?"
Practice
Solution
Step 1: Understand string comparison functions
MATLAB provides strcmp for case-sensitive string comparison and strcmpi for case-insensitive comparison.Step 2: Identify the correct function for case-sensitive comparison
strcmp compares strings exactly, including letter case, so it is the correct choice.Final Answer:
strcmp -> Option BQuick Check:
Case-sensitive comparison = strcmp [OK]
- Confusing strcmp with strcmpi
- Using non-existent functions like strcomp
- Assuming strcmpi is case-sensitive
s1 and s2 ignoring case in MATLAB?Solution
Step 1: Recall syntax for case-insensitive comparison
strcmpi compares two strings ignoring case and takes two string inputs as separate arguments.Step 2: Identify correct syntax
strcmpi(s1, s2) uses strcmpi with two string arguments correctly. Options C and D incorrectly use '==' inside the function, which is invalid.Final Answer:
strcmpi(s1, s2) -> Option CQuick Check:
Case-insensitive syntax = strcmpi(s1, s2) [OK]
- Using '==' inside strcmp or strcmpi
- Mixing up strcmp and strcmpi
- Passing strings as one argument with '=='
s1 = "Hello";
s2 = "hello";
result = strcmp(s1, s2);
disp(result);
Solution
Step 1: Understand strcmp behavior
strcmp compares strings with case sensitivity. "Hello" and "hello" differ in case.Step 2: Evaluate strcmp result
Since the strings differ in case, strcmp returns logical 0 (false), which disp prints as 0.Final Answer:
0 -> Option DQuick Check:
Case-sensitive mismatch = 0 [OK]
- Assuming strcmp ignores case
- Expecting output '1' for different case
- Confusing output with string 'true'
s1 = "Test";
s2 = "test";
if strcmpi(s1 == s2)
disp('Match');
end
Solution
Step 1: Analyze the use of '==' inside strcmpi
The expression s1 == s2 compares strings element-wise, returning a logical array, not a string.Step 2: Understand strcmpi input requirements
strcmpi expects two string inputs, not a logical array. Using '==' inside causes a syntax or runtime error.Final Answer:
Using '==' inside strcmpi causes an error -> Option AQuick Check:
strcmpi needs strings, not logical arrays [OK]
- Using '==' inside strcmpi
- Thinking strcmpi compares logical arrays
- Ignoring input type requirements
C1 = {'Apple', 'Banana', 'Cherry'} and C2 = {'apple', 'banana', 'cherry'} ignoring case. Which MATLAB code correctly returns a logical array indicating matches?Solution
Step 1: Understand cell array string comparison
To compare each pair of strings in cell arrays element-wise, use cellfun with a comparison function.Step 2: Choose case-insensitive comparison
strcmpi ignores case, so cellfun(@strcmpi, C1, C2) applies it element-wise correctly.Step 3: Why other options fail
cellfun(@strcmp, C1, C2) uses case-sensitive strcmp. strcmpi(C1{:}, C2{:}) unpacks cells into multiple arguments, causing a "too many input arguments" error. strcmp(C1, C2) is case-sensitive.Final Answer:
cellfun(@strcmpi, C1, C2) -> Option AQuick Check:
Use cellfun with strcmpi for cell array case-insensitive comparison [OK]
- Using strcmp instead of strcmpi for ignoring case
- Unpacking cell arrays with strcmpi(C1{:}, C2{:})
- Not using cellfun for element-wise comparison
Ready to prove it?
Pass the test and this pattern moves unknown
