Time complexity for comparing two lines

How does the ff function time work o(nlogn)?

function isPermutation(a, b) {
    if (a.length !== b.length) {
        return false;
    }
    return a.split("").sort().join() === b.split("").sort().join();
}

You do not check the length of both lines, or does it depend on the implementation of sorting?

+4
source share
1 answer

According to the definition of Permutation , String is a permutation of another string if and only if all characters in the first string are also in the second string.

Example : "answer"is a permutation "awerns".

So, to write an algorithm that checks if one line is a permutation of another line, all you have to do is:

  • Check if the length of the two lines is the same, return false if they do not match.

  • String , String.

O (n * n) :

  • , , false, .
  • char , stringOne[i] == stringTwo[i]

, , , Quick Sort Merge Sort, O (n * logn)

+1

Source: https://habr.com/ru/post/1615852/


All Articles