-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdi-string-match.cpp
More file actions
58 lines (41 loc) · 1.08 KB
/
Copy pathdi-string-match.cpp
File metadata and controls
58 lines (41 loc) · 1.08 KB
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
52
53
54
55
56
57
58
//question//
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.
Example 1:
Input: s = "IDID"
Output: [0,4,1,3,2]
Example 2:
Input: s = "III"
Output: [0,1,2,3]
Example 3:
Input: s = "DDI"
Output: [3,2,0,1]
Constraints:
1 <= s.length <= 105
s[i] is either 'I' or 'D'.
//solution//
class Solution {
public:
vector<int> diStringMatch(string s) {
vector<int> v;
int n = s.size();
int start = 0;
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='I')
{
v.push_back(start);
start++;
}
else
{
v.push_back(n);
n--;
}
}
v.push_back(start);
return v;
}
};