题目
实现一个算法,确定一个字符串 s
的所有字符是否完全都不同。
思路
两种方法
- 使用set去重之后比较去重后的长度和原始长度
- 使用集合(无序不重复 )保存遍历过的元素,一旦有重复就返回False。
class Solution:
def isUnique(self, astr: str) -> bool:
return len(set(astr)) == len(astr)
class Solution:
def isUnique(self, astr: str) -> bool:
no_repeat = set()
for ch in astr:
if ch in no_repeat:
return False
else:
no_repeat.add(ch)
return True