Skip to content

Instantly share code, notes, and snippets.

@monkerek
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkerek/f008f983280c2daf755d to your computer and use it in GitHub Desktop.
Save monkerek/f008f983280c2daf755d to your computer and use it in GitHub Desktop.
CC1.1
# idea: sort the string such that same characters sit together, then traverse the string one by one
# time complexity: O(NlogN)? depends on sorting alg.
# space complexity: O(1)
class Solution:
def UniqueChar(self, string):
if string == '':
return True
string = sorted(string)
prev = string[0]
for char in string[1:]:
if char == prev:
return False
prev = char
return True
@habina
Copy link

habina commented Jun 17, 2014

Sort it first, terrific idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment