How to insert characters in a string at a certain position?
Last Updated :
21 Sep, 2023
Given a string str and an array of indices chars[] that describes the indices in the original string where the characters will be added. For this post, let the character to be inserted in star (*). Each star should be inserted before the character at the given index. Return the modified string after the stars have been added.
Examples:
Input: str = “geeksforgeeks”, chars = [1, 5, 7, 9]
Output: g*eeks*fo*rg*eeks
Explanation: The indices 1, 5, 7, and 9 correspond to the bold characters in “geeksforgeeks”.
Input: str = “spacing”, chars = [0, 1, 2, 3, 4, 5, 6]
Output: “*s*p*a*c*i*n*g”
Approach: To solve the problem follow the below idea:
Iterate over the string and keep track of the count of the characters in the string so far and whenever your count becomes equal to the element in the array of stars, append a star to the resultant string and move ahead in your star array.
Follow the steps mentioned below to implement the idea:
- Create a string ans for storing your resultant string.
- Take one pointer j initially as 0.
- Iterate over the string and whenever your index that represents the count of characters becomes equal to the element in stars[j], append the star in your ans string and move the j pointer ahead.
- Also, at each move, append the current character in your string ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string addStars(string s, vector< int >& stars)
{
string ans = "" ;
int j = 0;
for ( int i = 0; i < s.length(); i++) {
if (j < stars.size() && i == stars[j]) {
ans += '*' ;
j++;
}
ans += s[i];
}
return ans;
}
int main()
{
string str = "geeksforgeeks" ;
vector< int > chars = { 1, 5, 7, 9 };
string ans = addStars(str, chars);
cout << ans << endl;
}
|
Java
import java.io.*;
class GFG
{
public static String addStars(String s, int stars[])
{
String ans = "" ;
int j = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (j < stars.length && i == stars[j]) {
ans += '*' ;
j++;
}
ans += s.charAt(i);
}
return ans;
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int chars[] = { 1 , 5 , 7 , 9 };
String ans = addStars(str, chars);
System.out.println(ans);
}
}
|
Python3
def addStars(s, stars) :
ans = "";
j = 0 ;
for i in range ( len (s)) :
if (j < len (stars) and i = = stars[j]) :
ans + = '*' ;
j + = 1 ;
ans + = s[i];
return ans;
if __name__ = = "__main__" :
string = "geeksforgeeks" ;
chars = [ 1 , 5 , 7 , 9 ];
ans = addStars(string, chars);
print (ans);
|
C#
using System;
using System.Collections;
public class GFG
{
public static string addStars( string s, int [] stars)
{
string ans = "" ;
int j = 0;
for ( int i = 0; i < s.Length; i++) {
if (j < stars.Length && i == stars[j]) {
ans += '*' ;
j++;
}
ans += s[i];
}
return ans;
}
public static void Main( string [] args)
{
string str = "geeksforgeeks" ;
int [] chars = { 1, 5, 7, 9 };
string ans = addStars(str, chars);
Console.Write(ans);
}
}
|
Javascript
function addStars(s, stars) {
let ans = "" ;
let j = 0;
for (let i = 0; i < s.length; i++) {
if (j < stars.length && i == stars[j]) {
ans += '*' ;
j++;
}
ans += s[i];
}
return ans;
}
let str = "geeksforgeeks" ;
let chars = [1, 5, 7, 9];
let ans = addStars(str, chars);
console.log(ans)
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach: using inbuilt insert function.
In this approach we need to increase the length of orignal string as on insert operation the orignal string get modified and so the target index needs to be increased by 1 so we used k.
The addStars function inserts an asterisk (*) at the positions specified in the chars vector. Here’s step by step approach for same:
- The addStars function takes a string s and a vector of integers stars as input.
- It iterates through the stars vector using a for loop.
- For each position specified in the stars vector, it inserts an asterisk (*) at that position in the string s(using insert function).
- we increment the k on insertion because size increases on an insertion operation.
- The updated string is returned.
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string addStars(string s, vector< int >& stars)
{
int k=0;
for ( int i = 0; i < stars.size(); i++) {
s.insert(stars[i]+ k++, "*" );
}
return s;
}
int main()
{
string str = "geeksforgeeks" ;
vector< int > chars = { 1, 5, 7, 9 };
string ans = addStars(str, chars);
cout << ans << endl;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class GFG {
public static String addStars(String s, List<Integer> stars) {
int k = 0 ;
for ( int i = 0 ; i < stars.size(); i++) {
s = s.substring( 0 , stars.get(i) + k) + "*" + s.substring(stars.get(i) + k);
k++;
}
return s;
}
public static void main(String[] args) {
String str = "geeksforgeeks" ;
List<Integer> chars = new ArrayList<>();
chars.add( 1 );
chars.add( 5 );
chars.add( 7 );
chars.add( 9 );
String ans = addStars(str, chars);
System.out.println(ans);
}
}
|
Python3
def addStars(s, stars):
k = 0
for i in range ( len (stars)):
s = s[:stars[i] + k] + "*" + s[stars[i] + k:]
k + = 1
return s
str = "geeksforgeeks"
chars = [ 1 , 5 , 7 , 9 ]
ans = addStars( str , chars)
print (ans)
|
C#
using System;
using System.Collections.Generic;
namespace CodeTranslation
{
class Program
{
static string AddStars( string s, List< int > stars)
{
int k = 0;
for ( int i = 0; i < stars.Count; i++)
{
s = s.Insert(stars[i] + k++, "*" );
}
return s;
}
static void Main( string [] args)
{
string str = "geeksforgeeks" ;
List< int > chars = new List< int > { 1, 5, 7, 9 };
string ans = AddStars(str, chars);
Console.WriteLine(ans);
}
}
}
|
Javascript
function addStars(s, stars) {
let k = 0;
for (let i = 0; i < stars.length; i++) {
s = s.slice(0, stars[i] + k) + "*" + s.slice(stars[i] + k);
k += 1;
}
return s;
}
let str = "geeksforgeeks" ;
let chars = [1, 5, 7, 9];
let ans = addStars(str, chars);
console.log(ans);
|
Time Complexity: O(N*K)
Auxiliary Space: O(N)
Explaination:
The time complexity of this approach is O(n*k), where n is the length of the string and k is the size of the vector. This is because for each position in the vector, we need to insert an asterisk into the string, which takes O(n) time.
The auxiliary space complexity is O(1), as we are not using any additional data structures.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...