异序词检测需求分析
# 异序词检测方法:
from collections import Counter
s1, s2, s3 = "acbde", "abced", "abcda"
c1, c2, c3 = Counter(s1), Counter(s2), Counter(s3)
if c1 == c2:
print('1和2是异序词')
if c1 == c3:
print('1和3是异序词')
else:
print('都不是')
# 清点法
def func(str1, str2):
if len(str1) != len(str2):
return False
for i in str1:
for j in str2:
if i == j and i != '#':
str1 = str1.replace(i, '#')
str2 = str2.replace(j, '#')
break
for n in str1:
if n != '#':
return False
for n in str2:
if n != '#':
return False
return True
str1 = 'heart'
str2 = 'earth'
print(func(str1, str2))
# 第二种写法 排序法
def dunc(s1, s2):
list1 = list(s1) # 转成列表
list2 = list(s2)
list1.sort() # 排序
list2.sort()
str1 = ''.join(list1) # list转成字符串
str2 = ''.join(list2)
return str2 == str1
s1 = 'Jge'
s2 = 'eJg'
print(dunc(s1, s2))
# 第三种 蛮力法 这个可能想不到 就不学了
# 第四种 计数法 认识ord函数 ASCII码 ord'a' 就会出现 ASCII码所对应的位置