Advertisement
makispaiktis

BinarySearchIO - A unique string

Aug 25th, 2020 (edited)
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. '''
  2. Given a string s, determine whether it has all unique characters.
  3. Example 1
  4. Input
  5. s = "abcde"
  6. Output
  7. True
  8. '''
  9.  
  10. def solve(s):
  11.     unique = list()
  12.     for i in range(len(s)):
  13.         if s[i] not in unique:
  14.             unique.append(s[i])
  15.     # Create a dictionary
  16.     dictionary = dict()
  17.     for char in unique:
  18.         dictionary[char] = 0
  19.     for i in range(len(s)):
  20.         for j in range(len(unique)):
  21.             if s[i] == unique[j]:
  22.                 dictionary[s[i]] += 1
  23.     # Now, we have the dictionary and the appearance of each unique letter
  24.     print(dictionary)
  25.     for key in dictionary:
  26.         if dictionary[key] != 1:
  27.             return False
  28.     return True
  29.  
  30.  
  31. # MAIN FUNCTION
  32. print(solve("abcde"))
  33. print(solve("abcdef"))
  34. print(solve("abcdeeeeea"))
  35. print(solve("abcdeAAd"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement