检查给定字符串是否为Heterogram的Python程序
这里给出一个字符串,然后我们的任务是检查给定字符串是否为Heterogram的天气。
八字组检查的含义是单词,短语或句子中,没有一个字母出现多次。可以将异义词与使用字母表中所有字母的七巧板区别开来。
示例
字符串是abcdefghi
This is Heterogram (no alphabet repeated)
字符串是abcbcddfh
This is not Heterogram. (b,c,d are repeated)
算法
Step 1: first we separate out list of all alphabets present in sentence. Step 2: Convert list of alphabets into set because set contains unique values. Step 3: if length of set is equal to number of alphabets that means each alphabet occurred once then sentence is heterogram, otherwise not.
范例程式码
def stringheterogram(s, n): hash = [0] * 26 for i in range(n): if s[i] != ' ': if hash[ord(s[i]) - ord('a')] == 0: hash[ord(s[i]) - ord('a')] = 1 else: return False return True # Driven Code s = input("Enter the String ::>") n = len(s) print(s,"This string is Heterogram" if stringheterogram(s, n) else "This string is not Heterogram")
输出结果
Enter the String ::> asd fgh jkl asd fgh jkl this string is Heterogram Enter the String ::>asdf asryy asdf asryy This string is not Heterogram