Matej Focko
decfe0c0b3
URL: https://leetcode.com/problems/adding-spaces-to-a-string/ Signed-off-by: Matej Focko <me@mfocko.xyz>
18 lines
397 B
Java
18 lines
397 B
Java
class Solution {
|
|
public String addSpaces(String s, int[] spaces) {
|
|
var result = new StringBuilder();
|
|
result.ensureCapacity(s.length() + spaces.length);
|
|
|
|
int i = 0;
|
|
for (int j = 0; j < s.length(); ++j) {
|
|
if (i < spaces.length && spaces[i] == j) {
|
|
result.append(' ');
|
|
++i;
|
|
}
|
|
|
|
result.append(s.charAt(j));
|
|
}
|
|
|
|
return result.toString();
|
|
}
|
|
}
|