Generate string after adding spaces at specific positions in a given String
Given a string s and an array spaces[] describing the original string’s indices where spaces will be added. The task is to add spaces in given positions in spaces[] and print the string formed.
Examples:
Input: s = “GeeksForGeeK”, spaces = {1, 5, 10}
Output: “G eeks ForGe eK”
Explanation: The underlined characters in “GeeksForGeeK” relate to the indices 1, 5, and 10. After that, put spaces in front of those characters.Input: s = “ilovegeeksforgeek”, spaces = {1, 5, 10, 13}
Output: “i love geeks for geek”
Approach: This problem is simple string implementation based. Follow the steps below to solve the given problem. Follow the steps below to solve the given problem.
- Initialize with a space in the new string of size of the sum of the length of both arrays.
- Visit the index and wherever the index is equal to the current space value in the space[] array skip it as space is already there.
- Else keep assigning the value from the main string
- Here addition of ‘l’ in line { if(l<N and i==sp[l]+l) } is very interesting to observe:
- The values in the space array are basically according to the old input string.
- But in the new string, these space values or indices are basically shifting by the number of spaces found before.
- Print the string formed at the end.
Below is the implementation of the above approach
C++
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to add space in required positions string spaceintegration(string s, vector< int >& sp) { int M = s.size(), N = sp.size(), l = 0, r = 0; string res(M + N, ' ' ); // Iterate over M+N length for ( int i = 0; i < M + N; i++) { if (l < N and i == sp[l] + l) l++; else res[i] = s[r++]; } // Return the required string return res; } // Driver Code int main() { string s = "ilovegeeksforgeeks" ; vector< int > space = { 1, 5, 10, 13 }; // Function Call cout << spaceintegration(s, space) << endl; return 0; } |
Java
// Java program for above approach import java.util.*; class GFG { // Function to add space in required positions static String spaceintegration(String s, int []sp) { int M = s.length(), N = sp.length, l = 0 , r = 0 ; String res = newstr(M + N, ' ' ); // Iterate over M+N length for ( int i = 0 ; i < M + N; i++) { if (l < N && i == sp[l] + l) l++; else res = res.substring( 0 ,i)+s.charAt(r++)+res.substring(i+ 1 ); } // Return the required String return res; } static String newstr( int i, char c) { String str = "" ; for ( int j = 0 ; j < i; j++) { str+=c; } return str; } // Driver Code public static void main(String[] args) { String s = "ilovegeeksforgeeks" ; int [] space = { 1 , 5 , 10 , 13 }; // Function Call System.out.print(spaceintegration(s, space) + "\n" ); } } // This code contributed by shikhasingrajput |
Python3
# Python3 program for above approach # Function to add space in required positions def spaceintegration(s, sp): M = len (s) N = len (sp) l = 0 r = 0 res = [ ' ' ] * (M + N) # Iterate over M+N length for i in range (M + N): if (l < N and i = = sp[l] + l): l + = 1 else : res[i] = s[r] r + = 1 # Return the required string return ''.join(res) # Driver Code if __name__ = = "__main__" : s = "ilovegeeksforgeeks" space = [ 1 , 5 , 10 , 13 ] # Function Call print (spaceintegration(s, space)) # This code is contributed by ukasp |
C#
// C# program for above approach using System; class GFG { // Function to add space in required positions static String spaceintegration(String s, int []sp) { int M = s.Length, N = sp.Length, l = 0, r = 0; String res = newstr(M + N, ' ' ); // Iterate over M+N length for ( int i = 0; i < M + N; i++) { if (l < N && i == sp[l] + l) l++; else res = res.Substring(0,i)+s[r++]+res.Substring(i+1); } // Return the required String return res; } static String newstr( int i, char c) { String str = "" ; for ( int j = 0; j < i; j++) { str+=c; } return str; } // Driver Code public static void Main() { String s = "ilovegeeksforgeeks" ; int [] space = { 1, 5, 10, 13 }; // Function Call Console.Write(spaceintegration(s, space) + "\n" ); } } // This code is contributed by Saurabh Jaiswal |
Javascript
<script> // JavaScript code for the above approach // Function to add space in required positions function spaceintegration(s, sp) { let M = s.length, N = sp.length, l = 0, r = 0; let res = new Array(M + N).fill( ' ' ); // Iterate over M+N length for (let i = 0; i < M + N; i++) { if (l < N && i == sp[l] + l) l++; else res[i] = s[r++]; } // Return the required string return res.join( '' ); } // Driver Code let s = "ilovegeeksforgeeks" ; let space = [1, 5, 10, 13]; // Function Call document.write(spaceintegration(s, space) + '<br>' ); // This code is contributed by Potta Lokesh </script> |
i love geeks for geeks
Time Complexity: O(M+N)
Auxiliary Space: O(M+N)