Locks O(n*m) solution replaced with O(n) version