-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay 22
20 lines (17 loc) · 820 Bytes
/
Day 22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
CODE:
class Solution {
public String addSpaces(String s, int[] spaces) {
StringBuilder result = new StringBuilder();
int spaceIndex = 0;
for (int i = 0; i < s.length(); i++) {
// Check if the current index matches a space position
if (spaceIndex < spaces.length && i == spaces[spaceIndex]) {
result.append(" "); // Add a space
spaceIndex++; // Move to the next space index
}
result.append(s.charAt(i)); // Append the current character
}
return result.toString();
}
}