-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2109-adding-spaces-to-a-string.js
More file actions
33 lines (30 loc) · 968 Bytes
/
2109-adding-spaces-to-a-string.js
File metadata and controls
33 lines (30 loc) · 968 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
32
33
/**
* 2109. Adding Spaces to a String
* https://leetcode.com/problems/adding-spaces-to-a-string/
* Difficulty: Medium
*
* You are 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.
* - For example, given s = "EnjoyYourCoffee" and spaces = [5, 9], we place spaces before 'Y'
* and 'C', which are at indices 5 and 9 respectively. Thus, we obtain "Enjoy Your Coffee".
*
* Return the modified string after the spaces have been added.
*/
/**
* @param {string} s
* @param {number[]} spaces
* @return {string}
*/
var addSpaces = function(s, spaces) {
let result = '';
let spaceIndex = 0;
for (let i = 0; i < s.length; i++) {
if (spaceIndex < spaces.length && i === spaces[spaceIndex]) {
result += ' ';
spaceIndex++;
}
result += s[i];
}
return result;
};