Time: O(m+n), Space: O(1)