检查给定字符串是否为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
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬