v2 time O(n) space O(n) Accepted

absent mind causes many bugs!