Adding Spaces to a String LeetCode solution in C++

1
class Solution {
2
public:
3
string addSpaces(string s, vector<int> &spaces) {
4
int i, n = spaces.size(), m = s.size(), j;
5
string ans = "";
6
i = 0;
7
j = 0;
8

9
// jth pointer for current index of spaces vector
10
// ith pointer for current index of our answer string
11
while (i < m) {
12
// if at our current index i is equals to spaces[j] which means we have
13
// space at this index,so we add space right here
14
if (j < n && i == spaces[j]) {
15
ans += " ";
16
j++;
17
// incrementing j to get next space index location
18
}
19
ans += s[i];
20
i++;
21
}
22

23
return ans;
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0