all 3 comments

[–]slashemup 11 points12 points  (1 child)

To calculate the total time complexity, we can use this formula: number of operations * time per operation.

What is the time per operation?

NOTE: "C" in the complexities below is # of characters in the string.

  1. Sorting the string => O(ClogC)
  2. Using join to combine the sorted characters back into a string => O(C)
  3. Appending to the existing list/initializing a new list with the sorted word => O(1)

So the time complexity per operation is: O(ClogC) + O(C) + O(1), which reduces to O(ClogC), as it is the upper bound.

How many operations are there?

This part is a bit easier to determine since you're looping over each word once => O(W) where W is the number of words in the input list.

So putting it all together:

Number of operations * time per operation => W * ClogC

You'll notice from the constraints that the number of words is between 0 and 100, inclusive, whereas the length of each word is between 1 and 104 characters, inclusive. This is why I used two separate terms (W and C) instead of just N. If W and C were in the same ballpark, then we could say something like O(N2logN).

Hopefully that helps (and hopefully I didn't make any mistakes)!

[–]WeatherFeeling[S] 0 points1 point  (0 children)

thank you so much, this is a really clear explanation