Suppose I have an array/list that consists of string elements like:
arr=[“abcd”,”abdd”,”abba”, “abca”] and I want to find the min and max element here (i.e. abba and abdd), what will be the time complexity of running a min(arr)/max(arr) function here.
I know for numerical elements we can do it O(n) as there’s just 1 comparison at every step.
But for when we compare 2 strings we have to iterate its characters to compare. So does that make it
somewhere in the order of O(n^2)??
Anirudh Kakati is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You only need to iterate the list once to compare two elements, current vs min and max. That’s 2*n comparisons, so still O(n)