![]() |
VOOZH | about |
Given a string s, a character c and an integer position pos, the task is to insert the character c into the string s at the specified position pos.
Examples:
Input: s = "Geeks", c = 'A', pos = 3
Output: GeeAksInput: s = "HelloWorld", c = '!', pos = 5
Output: Hello!World
We will use library methods like string insert in C++, StringBuilder insert in Java and String Slicing in Python.
GeeAks
Time Complexity: O(n) where n is the length of the string.
First, iterate through the given string, inserting all the characters into a new string until we reach the position where the given character needs to be inserted. At that position, insert the character, and then append the remaining characters from the input string to the new string.
GeeAks
Time Complexity: O(n) where n is the length of the string.