-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDI_string_match.py
More file actions
29 lines (23 loc) · 851 Bytes
/
DI_string_match.py
File metadata and controls
29 lines (23 loc) · 851 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
'''
A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where:
s[i] == 'I' if perm[i] < perm[i + 1], and
s[i] == 'D' if perm[i] > perm[i + 1].
Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.
'''
class Solution:
def diStringMatch(self, s: str) -> List[int]:
n = len(s)+1
n = [i for i in range(0, n)]
mini = min(n)
maxi = max(n)
total = list()
for i in range(len(s)):
if s[i] == "I":
total.append(mini)
mini += 1
elif s[i] == "D":
total.append(maxi)
maxi -= 1
total.append(maxi)
print(total)
return total