Check if a string contains all unique characters in Python

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

This Python Program will help you check “if a string contains all unique characters”

Python Program To Check If a string contains all unique characters

def isUniqueChars(st):
     if len(st) > 256:
         return False
     char_set = [False] * 128
     for i in range(0, len(st)):
         val = ord(st[i])
         if char_set[val]:
             return False
          char_set[val] = True
      return True
  # driver code
 st = "xyzd"
 print(isUniqueChars(st))

OutPut:

False

Note: Please change the value of st. i.e st= “abcd” and the Output will be “True”.

Leave a Comment

Your email address will not be published. Required fields are marked *