I was trying to find if the given strings are anagrams without using any helper sort function and nested loops.
Therefore I tried using a while loop; however, I am not sure what the big O notation of this code is. Can you please help?
def anagrams(string1, string2):
if len(string1) != len(string2):
return False
string3 = ""
x = 0
y = 0
while x < len(string1) and y < len(string2):
element = string1[x]
if element == string2[y]:
string3 += element
x += 1
y = 0
else:
y += 1
return string1 == string3
I believe it will be O(n^2) because your x value tends to increment, but your y value can continue to be reset to zero, therefore being no better than a nested loop.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments