Check if all items in a string are different?

4

How can I check if all items in a string are different?

For example:

x:"abcdefga" = False
y:"abcdefg" = True 

Since x[0] == x[7] , then it would be False .

But in this case I would use this condition in if .

Is there any function for this in python?

    
asked by anonymous 02.05.2017 / 01:37

1 answer

7

By set theory, a set data structure does not allow repeating elements. Therefore:

>>> x = "abcdefga"
>>> conjunto = set(x)
>>> conjunto
{'f', 'c', 'g', 'b', 'd', 'e', 'a'}

That is, if the comparison of len is equal between string and set, there are no repeating elements. Otherwise (set has fewer elements), there is some repetition:

len(x) == len(conjunto) # True se não há elementos repetidos. False caso contrário.
len(x) > len(conjunto) # True se há elementos repetidos. False caso contrário.
    
02.05.2017 / 01:50