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