I have a simple function, reverseWords(), that reveses the words in a string. eg. an input of a S = "this is a string" gives an output of "siht si a gnirts"
I was wondering what the big O of this function is. Is it O(N), O(N^2), or O(N* M)?
def reverseWords(S):
# code in Python 2.7
listA = S.split()
output = []
for element in listA:
output.append(element[::-1])
return (" ".join(output))
Is it O(N), O(N^2), or O(N* M)?
- O(N^2) because we have a nested loop eg. passing over input once, outer loop, and then reversing each character as the inner loop
- O(N*M) same reason as above except M is denoted as the looping over the characters
- O(N) because... I forgot the explanation