I solved the standard "write a method to determine if one string is a permutation of the other" question with the following code, using xor
, map
, and reduce
:
from functools import reduce
from operator import xor
def checkPermutation(str1, str2):
return not bool(reduce(xor, map(ord, str1 + str2)))
The idea is that if the two strings are permutations, the xor sum of the int value of every character of the concatenation of the two strings must be 0.
If I write the algorithm out by hand it seems come down to O(n)
time and space:
def checkPermutation(str1, str2):
# map every character in the strings to its integer value
map = []
str = str1 + str2
for char in str:
map.append(ord(char))
# xor every number that was mapped into a sum
sum = map[0]
for i in range(1, len(map)):
sum ^= map[i]
return not bool(sum)
Essentially I think that's what reduce
and map
are doing but depending on how they're actually implemented in python, the time and space complexity of two solutions may differ.
2 Answers 2
For both strings, generate a dictionary containing distinct characters of the string as keys and their respective counts as values.
If the two dictionaries are equal, the strings are permutations of each other.
Instead of manually counting characters using a dict
, you can utilize the collections.Counter
class. To save time in case the strings are of different length, you can compare their lengths first.
from collections import Counter
def checkPermutation(str1, str2):
return len(str1) == len(str2) and Counter(str1) == Counter(str2)
This is a \$O(n)\$ time and approximately \$O(1)\$ space solution. Approximately because – presumably – the less distinct characters there are in the input strings, the less space is occupied by the counters.
I guess it will be O(n)
in time, because you are stepping through the whole line and with n
elements. And this is the best case.
But it can be O(1)
in space because you need only room to store original line and an intermediate value which gets updated with every symbol in the line.
-
\$\begingroup\$ And how would that intermediate value be used? How precisely would it get updated? Would it really be \$O(1)\$? \$\endgroup\$kyrill– kyrill2017年04月07日 20:02:05 +00:00Commented Apr 7, 2017 at 20:02
-
1\$\begingroup\$ Just like topic starter stated above:
for el in s: sum ^= el
. It requiesn
steps to be done, and onlysizeof(el)
bytes to be stored. \$\endgroup\$Eugene Lisitsky– Eugene Lisitsky2017年04月08日 17:39:58 +00:00Commented Apr 8, 2017 at 17:39 -
1\$\begingroup\$ Yes, you could compute hashes first, and only if the hashes match you compare the strings. In worst case, you'll read the strings twice, but average performance would be improved. \$\endgroup\$kyrill– kyrill2017年04月08日 18:16:08 +00:00Commented Apr 8, 2017 at 18:16
Explore related questions
See similar questions with these tags.
xor
is not enough to be sure. The only thing you can say, ifxor(a) != xor(b)
then string aren't permutations. If they are equal, you can't say nothing definitely. \$\endgroup\$