-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApplyOperationsToMakeStringEmpty.py
51 lines (40 loc) · 1.46 KB
/
ApplyOperationsToMakeStringEmpty.py
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
45
46
47
48
49
50
51
# You are given a string s.
# Consider performing the following operation until s becomes empty:
# For every alphabet character from 'a' to 'z', remove the first occurrence of that character in s (if it exists).
# For example, let initially s = "aabcbbca". We do the following operations:
# Remove the underlined characters s = "aabcbbca". The resulting string is s = "abbca".
# Remove the underlined characters s = "abbca". The resulting string is s = "ba".
# Remove the underlined characters s = "ba". The resulting string is s = "".
# Return the value of the string s right before applying the last operation. In the example above, answer is "ba".
# For Biweekly Contest #124 Question 2
def lastNonEmptyString1(s: str) -> str:
while len(set(s)) != len(s):
removedSet = set()
newS = ""
for i in s:
if i not in removedSet:
removedSet.add(i)
else:
newS += i
s = newS
return s
def lastNonEmptyString(s):
s = s[::-1]
foundSet = set()
newS = ""
foundChar = dict()
for i in s:
if i not in foundChar:
foundChar[i] = 1
else:
foundChar[i] += 1
for i in s:
if i not in foundSet and foundChar[i] == max(foundChar.values()):
newS += i
foundSet.add(i)
return newS[::-1]
# Test cases
s = "aabcbbca"
print(lastNonEmptyString(s))
s = "abcd"
print(lastNonEmptyString(s))