#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有重復字符
運行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM