Algorithms and Data Structures · Middle · Question 71 of 75
What is the time complexity of str.split("").sort().join("") for a string of length n?
- 1O(log n)
- 2O(n)
- ✓Correct answer: O(n log n)
Explanation
Splitting and joining are O(n), but sorting the characters is O(n log n), which dominates. For short words it doesn't matter; for long strings counting characters is O(n).