String comparison and equality in C Sharp (C#) - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When comparing two strings, the time it takes depends on their length and content.
We want to know how the work grows as strings get longer.
Analyze the time complexity of the following code snippet.
string s1 = "hello world";
string s2 = "hello there";
bool areEqual = s1.Equals(s2);
This code checks if two strings are exactly the same.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing characters one by one in both strings.
- How many times: Up to the length of the shorter string, or until a difference is found.
As strings get longer, the comparison checks more characters.
| 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 roughly in direct proportion to the string length.
Time Complexity: O(n)
This means the time to compare grows linearly with the length of the strings.
[X] Wrong: "Comparing two strings always takes the same time regardless of their content."
[OK] Correct: The comparison stops early if a difference is found, so shorter matches can be faster.
Understanding how string comparison scales helps you reason about performance in many programs.
"What if we compare strings ignoring case? How would the time complexity change?"
Practice
str1 and str2 have the same value in C#?Solution
Step 1: Understand string equality operator
In C#,==compares the values of two strings correctly.Step 2: Analyze other options
str1 = str2is assignment,str1.Equalsis incomplete, andCompareToreturns an int, not a bool.Final Answer:
if (str1 == str2) -> Option AQuick Check:
Use==for string equality [OK]
- Using single = instead of ==
- Calling Equals without parentheses or arguments
- Using CompareTo expecting a boolean
a and b ignoring case in C#?Solution
Step 1: Identify case-insensitive comparison method
string.EqualswithStringComparison.OrdinalIgnoreCasecompares strings ignoring case.Step 2: Check other options
a == b.ToLower()compares different types,a.Equals(b)is case-sensitive, andstring.Comparereturns int, not bool.Final Answer:
string.Equals(a, b, StringComparison.OrdinalIgnoreCase) -> Option CQuick Check:
Use string.Equals with OrdinalIgnoreCase for case-insensitive [OK]
- Using == which is case-sensitive
- Calling Equals without StringComparison argument
- Using string.Compare expecting boolean
string s1 = "apple";
string s2 = "Banana";
int result = string.Compare(s1, s2, StringComparison.OrdinalIgnoreCase);
Console.WriteLine(result);
Solution
Step 1: Understand string.Compare with OrdinalIgnoreCase
It compares strings ignoring case and returns negative if first is before second alphabetically.Step 2: Compare "apple" and "Banana" ignoring case
"apple" comes before "banana" alphabetically, so result is negative (-1).Final Answer:
-1 -> Option AQuick Check:
"apple" < "Banana" ignoring case = -1 [OK]
- Assuming Compare returns boolean
- Ignoring case sensitivity in comparison
- Expecting 0 when strings differ
string a = "Hello";
string b = "hello";
if (a == b.ToLower())
{
Console.WriteLine("Equal");
} else {
Console.WriteLine("Not Equal");
}
Solution
Step 1: Analyze '==' operator behavior
The '==' operator compares strings case-sensitively, so "Hello" != "hello".Step 2: Understand why ToLower() doesn't fix it
Comparing 'a' to 'b.ToLower()' still compares case-sensitively; 'a' is "Hello" (mixed case), so comparison fails.Final Answer:
Using '==' compares case-sensitively, so it fails here -> Option DQuick Check:
'==' is case-sensitive, so this check fails [OK]
- Assuming ToLower() changes original string
- Using == for case-insensitive comparison
- Not calling Equals with StringComparison argument
x and y inside a custom comparer?Solution
Step 1: Understand sorting comparer requirements
A comparer must return negative, zero, or positive int based on alphabetical order.Step 2: Check each option's return value and case sensitivity
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); uses string.Compare with OrdinalIgnoreCase, correctly returning int for sorting ignoring case. return x == y ? 0 : 1; returns only 0 or 1, not suitable. return x.Equals(y) ? 0 : -1; returns 0 or -1 but ignores order and case. return x.CompareTo(y); uses CompareTo which is case-sensitive.Final Answer:
return string.Compare(x, y, StringComparison.OrdinalIgnoreCase); -> Option BQuick Check:
Use string.Compare with OrdinalIgnoreCase for case-insensitive sorting [OK]
- Returning only 0 or 1 instead of negative/zero/positive
- Using case-sensitive CompareTo for ignoring case
- Using Equals which returns bool, not int
