Tech for good

[CodePath] Unit 1: Strings and Arrays - Unique 본문

IT/Computer Science

[CodePath] Unit 1: Strings and Arrays - Unique

Diana Kang 2025. 6. 11. 07:26

def has_all_unique_characters(s):
    unique_chars = set(s)
    if len(s) == len(unique_chars):
        return True
    else:
        return False
 

Using Sets

  • Sets are collections of unique elements. You can convert a string into a set to get its unique characters.
  • The order of characters is not guaranteed in a set.
 
   text = "programming"
   unique_chars = set(text)
   print(unique_chars)  # Output: {'g', 'r', 'a', 'm', 'p', 'i', 'n', 'o'}