Lintcode 容易 python 判断字符串是否没有重复字符


判断字符串是否没有重复字符


实现一个算法确定字符串中的字符是否均唯一出现

样例

给出"abc",返回 true

给出"aab",返回 false

挑战 

如果不使用额外的存储空间,你的算法该如何改变?

标签 

数组  字符串处理  Cracking The Coding Interview

代码

class Solution:
    """
    @param: str: A string
    @return: a boolean
    """
    def isUnique(self, str):
        # write your code here
        a = list(str)#转化成列表
        n = len(a)
        for i in range(n):
            if str.count(a[i]) != 1: #判断单个字符串a[i]出现次数
                return False
                #break
        return True


你可能感兴趣的:(LintCode)