#2 判断一个字符串是否包含重复字符

「Google面试题」

【题目】

判断一个字符串是否包含重复字符。例如:“good”就包含重复字符‘o’,而“abc”就不包含重复字符

【题目分析】

对字符串进行遍历,统计每一个字符的个数,如果不为1则跳出遍历并返回True

【解答】

 1 #!/Users/minutesheep/.pyenv/shims/python
 2 # -*- coding: utf-8 -*-
 3 
 4 
 5 def isDup(strs):
 6     '''
 7     判断字符串是否有重复字符
 8     '''
 9     for ch in strs:
10     ¦   counts = strs.count(ch)
11     ¦   if counts > 1:
12     ¦   ¦   return True
13     return False
14 
15 
16 if __name__ == '__main__':
17     strs = 'Good'
18     result = isDup(strs)
19     print(strs + '有重复字符') if result else print(strs + '没有重复字符')
程序源代码
Good有重复字符
运行结果

猜你喜欢

转载自www.cnblogs.com/minutesheep/p/10390445.html