2810. Faulty Keyboard
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 78% Topics: String, Simulation
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
import collections
# deque
class Solution(object):
def finalString(self, s):
"""
:type s: str
:rtype: str
"""
dq = collections.deque()
parity = 0
for x in s:
if x == 'i':
parity ^= 1
else:
dq.appendleft(x) if parity else dq.append(x)
if parity:
dq.reverse()
return "".join(dq)
Solution from kamyu104/LeetCode-Solutions · MIT