Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d167a4b

Browse files
committed
Add solution #2109
1 parent 5b2bf97 commit d167a4b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,752 LeetCode solutions in JavaScript
1+
# 1,753 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1615,6 +1615,7 @@
16151615
2105|[Watering Plants II](./solutions/2105-watering-plants-ii.js)|Medium|
16161616
2106|[Maximum Fruits Harvested After at Most K Steps](./solutions/2106-maximum-fruits-harvested-after-at-most-k-steps.js)|Hard|
16171617
2108|[Find First Palindromic String in the Array](./solutions/2108-find-first-palindromic-string-in-the-array.js)|Easy|
1618+
2109|[Adding Spaces to a String](./solutions/2109-adding-spaces-to-a-string.js)|Medium|
16181619
2114|[Maximum Number of Words Found in Sentences](./solutions/2114-maximum-number-of-words-found-in-sentences.js)|Easy|
16191620
2115|[Find All Possible Recipes from Given Supplies](./solutions/2115-find-all-possible-recipes-from-given-supplies.js)|Medium|
16201621
2116|[Check if a Parentheses String Can Be Valid](./solutions/2116-check-if-a-parentheses-string-can-be-valid.js)|Medium|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 2109. Adding Spaces to a String
3+
* https://leetcode.com/problems/adding-spaces-to-a-string/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the
7+
* indices in the original string where spaces will be added. Each space should be inserted
8+
* before the character at the given index.
9+
* - For example, given s = "EnjoyYourCoffee" and spaces = [5, 9], we place spaces before 'Y'
10+
* and 'C', which are at indices 5 and 9 respectively. Thus, we obtain "Enjoy Your Coffee".
11+
*
12+
* Return the modified string after the spaces have been added.
13+
*/
14+
15+
/**
16+
* @param {string} s
17+
* @param {number[]} spaces
18+
* @return {string}
19+
*/
20+
var addSpaces = function(s, spaces) {
21+
let result = '';
22+
let spaceIndex = 0;
23+
24+
for (let i = 0; i < s.length; i++) {
25+
if (spaceIndex < spaces.length && i === spaces[spaceIndex]) {
26+
result += ' ';
27+
spaceIndex++;
28+
}
29+
result += s[i];
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)