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:
parent
6522258548
commit
decfe0c0b3
1 changed files with 18 additions and 0 deletions
18
java/adding-space-to-a-string.java
Normal file
18
java/adding-space-to-a-string.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue