Algorithms and Data Structures · Middle · Question 67 of 75
What is the complexity of checking whether two strings of length n are equal?
- 1Always O(1), strings are interned
- ✓Correct answer: O(n) in the worst case
- 3O(n²) because characters are copied
Explanation
Characters are compared until a difference is found, so equal or nearly equal strings cost O(n). This matters when strings are used as keys: hashing a key also reads the whole string.