def anagramSolution2s1s2 alist1 lists1 alist2 lists2 alist

def anagramSolution2(s1,s2):
alist1 = list(s1)
alist2 = list(s2)

alist1.sort()
alist2.sort()

pos = 0
matches = True

while pos < len(s1) and matches:
if alist1[pos]==alist2[pos]:
pos = pos + 1
else:
matches = False

return matches

include all operations and calculate the Big-O and the values for c and n0.

Solution

For the better understanfing of the complexity I will explain the complexity of each line one by one.

The order of times complexity of any algorithm is always the statement which is taking largest amount of time, because our focus is always on the worst case scenario.

We know that f(n)= O(g(n)) iff f(n)<=c*g(n) where c is a positive constant with c>=1

So time coopmlexity T(n) of the given code is

T(n)=O(max(n log n, m log m))., where m and n are length of strings s2 and s1 respectively. Value of c=1 and n0=1

def anagramSolution2(s1,s2): alist1 = list(s1) alist2 = list(s2) alist1.sort() alist2.sort() pos = 0 matches = True while pos < len(s1) and matches: if alist

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site