首页 > 其他分享 >Isograms

Isograms

时间:2023-04-25 18:58:37浏览次数:37  
标签:index lower string two len Isograms isogram

Details
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.
Example

 "Dermatoglyphics" = true
 "moose" = false
 "aba" = false

Solutions
mine

def is_isogram(string):
    if string:
        for index in range(len(string)):
            index_two = index + 1
            while index != len(string) - 1:
                if string[index].lower() == string[index_two].lower():
                    return False
                elif index_two <= len(string) - 2:
                    index_two += 1
                    continue

                break

    return True

Best:

def is_isogram(string):
    return len(string) == len(set(string.lower()))

set()函数是Python的内置函数之一,用于创建一个无序不重复元素集

标签:index,lower,string,two,len,Isograms,isogram
From: https://www.cnblogs.com/artwalker/p/17353489.html

相关文章