Given string str of size N, the task is to remove the characters present at odd indices (0-based indexing) of a given string.
Input: str = “abcdef”
The characters ‘b’, ‘d’ and ‘f’ are present at odd indices, i.e. 1, 3 and 5 respectively. Therefore, they are removed from the string.
Input: str = “geeks”
Approach: Follow the steps below to solve the problem:
- Initialize an empty string, say new_string, to store the result.
- Traverse the given string and for every index, check if it is even or not.
- If found to be true, append the characters at those indices to the string new_string.
- Finally, after complete traversal of the entire string, return the new_string.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes