2109. Adding Spaces to a String

T: O(s len+ space array size)

S: O(s len+ space array size)

class Solution {
    public String addSpaces(String s, int[] spaces) {
        StringBuilder sb = new StringBuilder();
        int start = 0;
        for (int space : spaces) {
            sb.append(s.substring(start, space)).append(" ");
            start = space;
        }
        sb.append(s.substring(start, s.length()));
        return sb.toString();
    }
}ja

Last updated