我的任务是编写一个函数,返回两个字符串是否为彼此的substitution ciphers.假设没有给一个人一把 keys .预计输出将返回True或False.
这里给了我几个测试用例(分别为string1
和string2
):
- 香蕉/可乐-->;True
- 狗/cat -->;True
- 香蕉/科尔-->;False
以下是我到目前为止在这个问题上所写的(borrow 自CodeFights的一个问题).其思想是将字符串中每个元素的计数附加到string1count
和string2count
变量中.然后,比较每个索引处的计数,如果它们不相等,我们可以假定它不是有效的替代密码,因为数组中的每个元素需要具有相同数量的对应字符才能成为替代密码.
def isSubstitutionCipher(string1, string2):
string1count = []
string2count = []
for i in range(0,len(string1)):
string1count.append(string1.count(string1[i]))
for i in range(0,len(string2)):
string2count.append(string2.count(string2[i]))
for i in range(0,len(string1count)):
if string1count.count(string1count[i])!=string2count.count(string1count[i]):
return False
return True
Does anyone else have other proposals on how to solve this very general question / problem statement?个