Adding Spaces to a String LeetCode solution in C++
1class Solution {2public:3string addSpaces(string s, vector<int> &spaces) {4int i, n = spaces.size(), m = s.size(), j;5string ans = "";6i = 0;7j = 0;89// jth pointer for current index of spaces vector10// ith pointer for current index of our answer string11while (i < m) {12// if at our current index i is equals to spaces[j] which means we have13// space at this index,so we add space right here14if (j < n && i == spaces[j]) {15ans += " ";16j++;17// incrementing j to get next space index location18}19ans += s[i];20i++;21}2223return ans;24}25};