# find all possible sorted substrings of s
substr = ["".join(sorted(s[i: j]))
for i in range(len(s))
for j in range(i + 1, len(s) + 1)]
I know the sorted()
method is O(nlog(n)), and the finding of all possible substrings is O(n^2). However, the .join()
is what is throwing me off. sorted()
returns a list and .join()
iterates over each element in the list to append it to a string. So it should be a linear operation.
Would my substring sorter therefore be O(n^2) for the nested loop * O(nlogn) for sorting each result * O(n) for joining? Ergo O(n^4logn)??
If so, would breaking up the operations make this more efficient? I had another implementation where I move the sorting of the substrings to a second list comprehension
substr = [s[i: j] for i in range(len(s))
for j in range(i + 1, len(s) + 1)]
sortedSubstr = ["".join(sorted(s)) for s in substr]
This would make it the O(n^2) list comprehension first + O(n)*O(nlogn) for the second list comprehension
Making the overall program now O(n^2logn)
Please correct me if I am wrong. Thanks