使用Python实现: 1.将两个单词转换为集合类型。 2.使用集合的交集操作,得到两个集合中的相同元素。 3.判断交集的长度是否大于0,若大于0则说明两个单词中有相同的字母。
代码示例:
def compare_word(word1, word2):
set1 = set(word1)
set2 = set(word2)
common_set = set1 & set2
if len(common_set) > 0:
return True
else:
return False
示例测试:
print(compare_word('hello', 'world')) # False
print(compare_word('hello', 'hi')) # True