Skip to content

Commit 620c4b4

Browse files
authored
Create di-string-match.cpp
1 parent cb8c429 commit 620c4b4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

C++/di-string-match.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> diStringMatch(string S) {
7+
vector<int> result;
8+
int left = 0, right = S.length();
9+
for (const auto& c : S) {
10+
if (c == 'I') {
11+
result.emplace_back(left);
12+
++left;
13+
} else {
14+
result.emplace_back(right);
15+
--right;
16+
}
17+
}
18+
result.emplace_back(left);
19+
return result;
20+
}
21+
};

0 commit comments

Comments
 (0)