「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有重復字符
