检查给定字符串是否为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 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短