Alternate Character Extraction

Easy
0/40
1 upvote
Asked in company
PayU

Problem statement

You are given a string S which may contain one or more words separated by spaces. Your task is to transform this string based on a specific set of rules.


The transformation process is as follows:

Split: The input string S is first split into parts (words) based on spaces.

Rearrange: For each part, you will create a new string by first  taking all characters at even indices (0, 2, 4, ...) in the order they appear, and then appending all characters at odd indices (1, 3, 5, ...) in the order they appear.

Combine: The transformed parts are then joined back together with a single space between them to form the final output string.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and only line of input contains a single string S.


Output Format:
The function should return the final transformed string.


Note:
If the input string contains no spaces, it is treated as a single part.

If there are multiple spaces between words, they should be treated as a single space delimiter when joining the final parts.

Python's string slicing (string[start:stop:step]) is particularly useful for this task.
Sample Input 1:
Hello World


Sample Output 1:
Hloel Wrdol


Explanation for Sample 1:
Part 1: "Hello"
  Even-indexed characters: 'H', 'l', 'o' -> "Hlo"
  Odd-indexed characters: 'e', 'l' -> "el"
  Combined part: "Hloel"
Part 2: "World"
  Even-indexed characters: 'W', 'r', 'd' -> "Wrd"
  Odd-indexed characters: 'o', 'l' -> "ol"
  Combined part: "Wrdol"
Final Result: "Hloel Wrdol"


Sample Input 2:
Vaibhav


Sample Output 2:
Vihvaba


Explanation for Sample 2:
The string is treated as a single part: "Vaibhav".
Even-indexed characters: 'V', 'i', 'h', 'v' -> "Vihv"
Odd-indexed characters: 'a', 'b', 'a' -> "aba"
Combined result: "Vihvaba"


Expected Time Complexity:
The expected time complexity is O(N).


Constraints:
1 <= length of S <= 10^5
The string S can contain any standard ASCII characters.

Time limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Alternate Character Extraction
Full screen
Console