A unique string in Python

PythonServer Side ProgrammingProgramming

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
raja
Published on 02-Sep-2020 16:30:57
Advertisements