forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
31 lines (30 loc) · 714 Bytes
/
solution.cpp
File metadata and controls
31 lines (30 loc) · 714 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
class Solution {
public:
vector<int> diStringMatch(string S) {
int counti = 0, countd = 0;
for(auto c:S){
if(c == 'I')
counti++;
}
countd = S.size()-counti;
vector<int> A(S.size()+1);
int iflag = countd, dflag = countd-1;
if(S[0] == 'I'){
A[0] = 0;
iflag ++;
dflag ++;
}else{
A[0] = A.size()-1;
}
for(int i=0;i<S.size();i++){
if(S[i] == 'I'){
A[i+1] = iflag;
iflag += 1;
}else{
A[i+1] = dflag;
dflag -= 1;
}
}
return A;
}
};