Skip to content

Commit b508f71

Browse files
committed
2109. Adding Spaces to a String
1 parent 4cc5cab commit b508f71

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Solution by Sergey Leschev
2+
// 2109. Adding Spaces to a String
3+
4+
// Two Pointers
5+
// We go through the string, tracking the position in the spaces array.
6+
7+
function addSpaces(s: string, spaces: number[]): string {
8+
let result = ''
9+
let spaceIndex = 0
10+
11+
for (let i = 0; i < s.length; i++) {
12+
if (spaceIndex < spaces.length && i === spaces[spaceIndex]) {
13+
result += ' '
14+
spaceIndex++
15+
}
16+
result += s[i]
17+
}
18+
19+
return result
20+
}

0 commit comments

Comments
 (0)