java: add «2109. Adding Spaces to a String»

URL:	https://leetcode.com/problems/adding-spaces-to-a-string/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-04 00:28:01 +01:00
parent 6522258548
commit decfe0c0b3
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,18 @@
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();
}
}