-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaUniqueString.py
More file actions
47 lines (26 loc) · 912 Bytes
/
aUniqueString.py
File metadata and controls
47 lines (26 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Iterating thorugh a dict of letters, relatively slow
def aUniqueString(s):
alphString = 'abcdefghijklmnopqrstuvwxyz'
alph = {}
for i in alphString:
alph[i] = 0
for i in s:
alph[i] = alph[i] + 1
for i in alph:
if alph[i] > 1:
return False
return True
# Iterating through string with each character, slightly faser
def aUniqueString2(s):
for i in range(len(s)):
for j in range(i + 1, len(s)):
if s[j] == s[i]:
return False
return True
# Use sorted() to place the same letters together
def aUniqueString3(s):
sorted(s)
for i in range(len(s) - 1): # The length of string - 1 so we won't get IndexOutOfBounds on the last iteration
if s[i] == s[i + 1]:
return False
return True