
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate String After Adding Spaces at Specific Positions
In this problem, we need to add spaces before the given indexes in the string. We can use two different approaches to solve the problem. The first approach moves the characters of the given string to add space at a particular index, and the second approach replaces the characters of the pre?initialize string with spaces.
Problem statement ? We have given string str of length N containing the alphanumeric characters. Also, we have given the spaceList array containing M positive integers representing the string index. We need to app space to the string before each index that exists in the array. After that, we need to print the updated string.
Sample examples
Input
str = "welcometotutorialspoint", spaceList = {7, 9}
Output
?welcome to tutorialspoint'
Explanation ? Here, we consider the 0?based indexing. We added the space before the 7th and 9th index in the input string.
Input
str = "abc", spaceList = {1, 2}
Output
?a b c'
Explanation ? We added the space before indexes 1 and 2.
Input
str = "IloveCProgramming", spaceList = {1, 5, 6}
Output
I love C Programming
Explanation ? We have seprated the string by adding space at given indexes.
Approach 1
In this approach, we will take a particular index from the array. After that, we will add space at the end of the string and move all characters right by 1, starting from the current index to the end of the string.
Algorithm
Step 1 ? Initialize the size variable with the size of the string.
Step 2 ? Use the loop to traverse the string.
Step 3 ? Get the index from the spaceList array.
Step 4 ? Append the space at the end of the given string.
Step 5 ? Replace all str[i+1] characters with str[i]. Here ?I' starts from the ?strLen' and goes until it becomes equal to the index ? 1.
Step 6 ? Assign space to the str[index ? 1].
Step 7 ? At last, return str, which is an updated string.
Example
#include <bits/stdc++.h> using namespace std; string AddSpaces(string str, vector<int> &spaceList) { // get the size of the list int size = spaceList.size(); // traverse the space list while (size--) { // get index to add space int index = spaceList[size] + 1; int strLen = str.size() - 1; // append space at the end str += ' '; // Move space for (int i = strLen; i >= index - 1; i--) { str[i + 1] = str[i]; } str[index - 1] = ' '; } return str; } int main() { string str = "welcometotutorialspoint"; vector<int> spaceList = {7, 9}; cout << "The updated string is : " << AddSpaces(str, spaceList) << endl; return 0; }
Output
The updated string is : welcome to tutorialspoint
Time complexity? O(N*K) where K is the size of the list and N is the size of the string.
Space complexity ? O(1) as we modify the same string.
Approach 2
In this approach, we initialize the string containing total spaces equal to string length + list length. After that, we update the string by replacing it with the input string's character and leave it as it is if we need to add spaces.
Algorithm
Step 1 ? Initialize the ?finalStr' string with the empty string. Also, the string length should be equal to the str_size and list_size. Here, str_size is the size of the string, and list_size is the size of the list.
Step 2 ? Traverse the finalStr string.
Step 3 ? If l_index is less than list size, and p is equal to the spaceList[l_index] + l_index, increase the l_index by 1 as we need to add space at that position.
Step 4 ? Else, assign str[index] to the finalStr[p] and increase the value of the index by 1.
Step 5 ? Return the ?finalStr' string.
Example
#include <bits/stdc++.h> using namespace std; string AddSpaces(string str, vector<int> &spaceList) { int str_size = str.size(), list_size = spaceList.size(), l_index = 0, s_index = 0; string finalStr(str_size + list_size, ' '); // Iterate over M+N length for (int p = 0; p < str_size + list_size; p++) { if (l_index < list_size and p == spaceList[l_index] + l_index) l_index++; else finalStr[p] = str[s_index++]; } // Return the required string return finalStr; } int main() { string str = "welcometotutorialspoint"; vector<int> spaceList = {7, 9}; cout << "The updated string is : " << AddSpaces(str, spaceList) << endl; return 0; }
Output
The updated string is : welcome to tutorialspoint
Time complexity? O(N + K) as we traverse the list and string together.
Space complexity ? O(N + K) as we initialize the finalStr with empty spaces.
The second solution is more optimized in the time complexity manner as it traverses the list and string together. The first approach traverses the string for every element of the list. Programmers can also try to insert the space after string index rather than before the string index.