unique_characters.py 498 B

12345678910111213141516171819202122
  1. # Implement an algorithm to determine if a string has all unique characters.
  2. # What if you can not use additional data structures?
  3. import pytest
  4. def isUnique(input):
  5. input_len = len(input)
  6. sub_string = {}
  7. for i in range(input_len):
  8. if input[i] in sub_string:
  9. return False
  10. else:
  11. sub_string[input[i]] = True
  12. return True
  13. def test_unique():
  14. input1 = 'asdfghjkl'
  15. input2 = 'aaaawwwwf'
  16. assert isUnique(input1) == True
  17. assert isUnique(input2) == False
  18. pytest.main()