I solved the standard "write a method to determine if one string is a permutation of the other" question with the following code, using
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 for i in range(1, len(map)): sum ^= map[i] return not bool(sum)
Essentially I think that's what
map are doing but depending on how they're actually implemented in python, the time and space complexity of two solutions may differ.