![]() |
VOOZH | about |
Given a string S along with an integer i. Then your task is to remove ith character from S.
Examples:
Input: S = Hello World!, i = 7
Output: Hello orld!
Explanation: The Xth character is W and after removing it S becomes Hello orld!Input: S = GFG, i = 1
Output: GG
Explanation: It can be verified that after removing 1st character the string S will be GG.
Below table represents inbuilt functions used to remove the i-th character from a string in different languages:
| Language | Inbuilt Function(s) |
|---|---|
| C++ | std::string::erase |
| Java | StringBuilder.deleteCharAt |
| Python | String slicing (str[:i] + str[i+1:]) |
| C# | string.Remove |
| JavaScript | String slicing (str.slice(0, i) + str.slice(i+1)) |
Below is the implementation:
Hello, orld!
Time Complexity: O(N)
Auxiliary Space: O(1)
We can create a new string and add all the characters of original input string except the ith character.
Below is the implementation of the above approach:
Hllo
Time Complexity: O(N)
Auxiliary Space: O(N)