Space Complexity O(n), Time Complexity O(mn)