检查给定字符串是否为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
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短