aboutsummaryrefslogtreecommitdiff
path: root/add_space_string.cpp
blob: 9a9b7ea2942c9ab9dd4521ede393d9a129f60e3c (plain)
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
// 2109. Adding Spaces to a String
#include "leetcode.h"

/*
 * given a 0-indexed string 's' and a 0-indexed integer array 'spaces' that
 * describes the indices in the original string where spaces will be added. each
 * space should be inserted before the character at the given index. return the
 * modified string after the spaces have been added
 */

class Solution {
public:
  string addSpaces(string s, vector<int> &spaces) {
    int n = spaces.size(), m = s.size(), i = 0, j = 0;
    string ans = "";
    while (i < m) {
      if (j < n && i == spaces[j]) {
        ans += ' ';
        j++;
      }
      ans += s[i];
      i++;
    }
    return ans;
  }
};

int main() {
  Solution obj;
  vector<int> s1 = {8, 13, 15}, s2 = {1, 5, 7, 9}, s3 = {0, 1, 2, 3, 4, 5, 6};
  cout << obj.addSpaces("LeetcodeHelpsMeLearn", s1)
       << endl; // expect: Leetcode Helps Me Learn
  cout << obj.addSpaces("icodeinpython", s2)
       << endl;                                 // expect: i code in py thon
  cout << obj.addSpaces("spacing", s3) << endl; // expect:  s p a c i n g
}