Confused about the big O notation of the code

user4592697

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
Randy Kamradt Sr.

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Still sort of confused about Big O notation

From Dev

Confused on big O notation

From Dev

Confused about big-O notation (specific example)

From Dev

I am confused about Big O notation and the rules

From Dev

I am confused about Big O notation and the rules

From Dev

Learning about Big O notation with C++, confused as to whether this is O(n) or O(n2)

From Dev

Confused about these asymptotic notation and its runtime

From Dev

Big O Notation for Algorithm

From Dev

Big O notation - recursion

From Dev

Big O Notation: Definition

From Dev

Meaning of Big O notation

From Dev

Big o notation work

From Dev

Big O notation Algorithm

From Dev

How to find complexity of this nested for loop code according to Big O notation?

From Dev

Confused about the following 'generics' code

From Dev

Big O Notation Of Exponential Functions

From Dev

What is the big o notation of following

From Dev

Big-O notation and polynomials?

From Dev

Complexity of this algorithm in big O notation?

From Dev

Big O Notation with Absolute Value?

From Dev

Big-O notation confusion

From Dev

Big O notation of three for loops

From Dev

Big O Notation - Growth Rate

From Dev

Determining the BIg O notation of this loop

From Dev

What is the big O notation for this algorithm?

From Dev

showing that a function is O(something) (big O notation)

From Dev

showing that a function is O(something) (big O notation)

From Dev

Is my thinking for the total number of operations in the code-snippet below valid? (Big-O notation)

From Dev

Confused about this php code using curly braces