An antique mechanical typewriter has a manufacturing defect: its ribbon carriage secretly reverses direction every time the letter i is struck. You are given the sequence of keys pressed as a string s of lowercase English letters and digits. Process each character of s in order: if the character is the letter i, the typewriter prints nothing -- instead its carriage reverses, so the text printed so far on the page becomes read in the opposite order (the entire currently printed text is reversed in place); for every other character, the machine prints that character normally, appended immediately after the text currently on the page. Determine the text that ends up printed on the page once every key in s has been processed.
A single line containing the string s.
Print the final text on the page after processing every character of s in order.
Example 1
Input
abicd
Expected
bacd
Explanation
Type 'a' -> page is 'a'. Type 'b' -> page is 'ab'. Type 'i' -> the ribbon reverses the page to 'ba' (nothing is printed for the 'i' itself). Type 'c' -> appended, page is 'bac'. Type 'd' -> appended, page is 'bacd'. Final output: bacd.
Example 2
Input
hi9i2
Expected
9h2
Explanation
Type 'h' -> page is 'h'. Type 'i' -> reversing a single character does nothing, page stays 'h'. Type '9' -> appended, page is 'h9'. Type 'i' -> reverses to '9h'. Type '2' -> appended, page is '9h2'. Final output: 9h2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →