-
Notifications
You must be signed in to change notification settings - Fork 0
/
q008.py
22 lines (19 loc) · 805 Bytes
/
q008.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# repost the first occurence of each character in a string
# the returned string should contain the unique characters in order of first occurence
# s : string
# return : the string containing the unique characters in order of first occurence
def first_occur(s):
out = ''
seen = set() # need the set to ensure that we do not use a character twice
# while guarnteeing constant look up times
for c in s:
if not c in seen:
seen.add(c)
out += c
return out
# this method runs in O(n) with n being the length of s, we iterate once over s and
# ensure that our checks are constant time by using a set
# follow up question:
# the return string should contain the unique characters in order of last occurence
def last_occur(s):
return first_occur(s.reverse()).reverse()