Ideas:
Let's first malloc an array-arr to store the elements and spaces of s. How big should this array be?
First of all, we can calculate the size of the s array - len, or directly get the size of spaces - that is, the number of spaces to be added, so is the size of the array we want to create len+spacesSize? No, it should be len+spacesSize+1, here The plus one is to store a '\n'
Here we can write a for i loop to put the elements in s into arr, and then define a falg outside to serve as the subscript of arr, store an element falg++, and then define a j=0, when i == space[j] stores a space, and stores a space ++.
Code (written in C language in Lituo):
char * addSpaces(char * s, int* spaces, int spacesSize){
int i = 0;
int j = 0;
int falg = 0;
int len = strlen(s);
char* arr = (char*)malloc((sizeof(char)) *(len + spacesSize + 1));
for(i = 0; i < len; i++)
{
if(j < spacesSize && i == spaces[j])
{
j++;
arr[falg++] = ' ';
}
arr[falg++] = s[i];
}
arr[falg] = '\0';
return arr;
}