Improve Article Show Save Article 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:
Approach#1: This problem is simple string implementation based. Follow the steps below to solve the given problem.
Below is the implementation of the above approach C++
Java
Python3
C#
Javascript
Output i love geeks for geeks Time Complexity: O(M+N) Approach#2: This problem can be solve by method which we use to insert element at specific position in array. Follow the steps below to solve the given problem.
Bellow is the implementation of above approach. C++
Python
C#
Javascript
Output i love geeks for geeks Time Complexity: O(M*N) Here M is the length of the string and N is the size of space vector. Auxiliary Space: O(1) As constant extra space is used. How do you add a space in Java?One way to do this is with string format codes. For example, if you want to pad a string to a certain length with spaces, use something like this: String padded = String. format("%-20s", str);
Can we use \t in Java?For a complete listing of all methods in this class (there are more than 50), refer to the java.
...
Escape Sequences.. How do you give a tab space in Java?you can give tab space in java easily. "\t" is the space sequence in java programming.
How many spaces does \t do in Java?Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).
|