Determines if leftString is a permutation (rearrangement of characters) of the rightString. If so, return true; otherwise return false.

Example

For leftString = "abc" and rightString = "bca", the output should be

IsPermutationOf(leftString, rightString) = true.

Input/Output

[time limit] 4000ms (js)

[input] string leftString

The left string to be checked.

[input] string rightString

The right string to be checked.

[output] boolean

Returns true if leftString is a permutation of rightString (and, of course, vice versa) and false otherwise.

Solution

xor-ing the ASCII values of a string results in a certain checksum value. As the order of the arguments to xor does not matter, the same value will emerge for an arbitrary permutation. When the same string is xor'ed twice, the result must be zero - which is equivalently true for a permutation.

If we want to check if two strings are permutations of each other, all we have to do is make sure the length is the same and check if the concatenated xor-checksum is zero.

Using the Buffer and the spread-operator again allows a fairly compact solution:

When we think about the length check, which consumes quite a few bytes, the only way to produce false positives is by adding test cases with an even number of additional characters on one string. Luckily, the questioner did not add such cases, which allows us to remove the check in order to pass the problem:

That's short but not satisfying at all, since we lost the generality. But we can think differently about the problem: If we would sort the characters of each string and compare the resulting strings again, we would have a solid check again. So