lintcode练习- 157. 判断字符串是否没有重复字符

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

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

样例

给出"abc",返回 true

给出"aab",返回 false

挑战

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

解决思路:

method 1: 利用hash表来存储字符,如果重复就返回,T(n),S(n)

method 2: 利用str.count()函数,T(n),没有额外的存储空间

class Solution:
    """
    @param: str: A string
    @return: a boolean
    """
    
    '''
    #利用hash表
    def isUnique(self, str):
        # write your code here
        hashmap = {}
        for item in str:
            if item in hashmap:
                return False
            hashmap[item] = 1
        
        return True
    '''
    def isUnique(self, str):
        # write your code here
        for item in str:
            if str.count(item) != 1:
                return False

        return True
    

猜你喜欢

转载自blog.csdn.net/qq_36387683/article/details/81750769