2109. Adding Spaces To A String
Intuition¶
To solve this problem, we can process the input string and insert spaces at the specified indices efficiently
Approach: Two Pointers¶
Using two pointers, we can:
- Use one pointer to iterate over the input string
s. - Use another pointer to keep track of the current position in the
spacesarray. - If the current character index matches the current space index from
spaces, append a space to the result string and move the pointer in thespacesarray. Otherwise, append the character to the result. - Append the remaining characters in s after processing all indices in spaces.
Explanation:¶
-
Initialization:
-
Use
ansto store the resulting string after inserting spaces. -
Keep a pointer
currSpaceto track which space index to process next. -
Iterate Through the String:
-
For each character in s, check if the current index matches the next space index in spaces.
- If it does, append a space to
ansand move thecurrSpacepointer. -
Append the current character to
ans. -
Return result::
-
Convert the
ansslice of bytes to a string and return it.
Complexity¶
- Time complexity: $O(n + m)$ where:
- n is the length of the string
s. - m is the length of the
spacesarray. - Space complexity: $O(n + m)$ where
n + mis the length ofansarray.
Code¶
go []
func addSpaces(s string, spaces []int) string {
a := make([]uint8, 0, len(s) + len(spaces))
for i, j := 0, 0; i < len(s); i++ {
if j < len(spaces) && spaces[j] == i {
a = append(a, ' ')
j++
}
a = append(a, s[i])
}
return string(a)
}