A unique string in Python


Suppose we have a string s, we have to check whether it has all unique characters or not.

So, if the input is like "world", then the output will be True

To solve this, we will follow these steps −

  • set_var := a new set from all characters of s

  • return true when size of set_var is same as size of s, otherwise false

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, s):
      set_var = set(s)
      return len(set_var) == len(s)
ob = Solution()
print(ob.solve('hello'))
print(ob.solve('world'))

Input

hello
world

Output

False
True

Updated on: 02-Sep-2020

637 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements