CodeWars 判断单词中是否有重复字符
注册 CodeWars 很多年了,却停留在 8kyu 段位,实在愧对父老乡亲,近来新学了 python,用来爬段位+巩固基础挺合适。
段位:7kyu
题目:判断单词中是否有重复字符
An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
is_isogram("Dermatoglyphics" ) == true
is_isogram("aba" ) == false
is_isogram("moOse" ) == false # -- ignore letter case
python 解法
def is_isogram(word):
return len(word) == len(set(word.lower()))
思路
利用集合的元素唯一特性,将目标单词中的字符去重,再比较集合与目标单词的长度