📌  相关文章
📜  在给定字符串的特定位置添加空格后生成字符串

📅  最后修改于: 2022-05-13 01:57:08.475000             🧑  作者: Mango

在给定字符串的特定位置添加空格后生成字符串

给定一个字符串s和一个数组spaces[]描述原始字符串的索引,其中将添加空格。任务是在空格[]中的给定位置添加空格并打印形成的字符串。

例子:

方法:这个问题是基于简单的字符串实现的。请按照以下步骤解决给定的问题。请按照以下步骤解决给定的问题。

  • 用两个数组长度之和大小的新字符串中的空格进行初始化。
  • 访问索引,只要索引等于space[]数组中的当前空间值,就跳过它,因为空间已经存在。
  • 否则继续分配主字符串中的值
  • 在 { if(l } 行中添加'l'非常有趣:
    • 空间数组中的值基本上是根据旧的输入字符串。
    • 但是在新的字符串中,这些空间值或索引基本上是按照之前找到的空格数移动的。
  • 打印最后形成的字符串。

下面是上述方法的实现

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to add space in required positions
string spaceintegration(string s, vector& 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 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


输出:
i love geeks for geeks

时间复杂度: O(M+N)
辅助空间: O(M+N)