diff --git a/java/adding-space-to-a-string.java b/java/adding-space-to-a-string.java new file mode 100644 index 0000000..ff545ec --- /dev/null +++ b/java/adding-space-to-a-string.java @@ -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(); + } +}