@author jackzhenguo
@desc 
@date 2019/9/3
排序词(permutation):两个字符串含有相同字符,但字符顺序不同。
from collections import defaultdict
def is_permutation(str1, str2):
    if str1 is None or str2 is None:
        return False
    if len(str1) != len(str2):
        return False
    unq_s1 = defaultdict(int)
    unq_s2 = defaultdict(int)
    for c1 in str1:
        unq_s1[c1] += 1
    for c2 in str2:
        unq_s2[c2] += 1
    return unq_s1 == unq_s2
这个小例子,使用python内置的defaultdict,默认类型初始化为int,计数默次数都为0. 这个解法本质是 hash map lookup
统计出的两个defaultdict:unq_s1,unq_s2,如果相等,就表明str1、 str2互为排序词。
下面测试:
r = is_permutation('nice', 'cine')
print(r)  # True
r = is_permutation('', '')
print(r)  # True
r = is_permutation('', None)
print(r)  # False
r = is_permutation('work', 'woo')
print(r)  # False