updated solution to leetcode problem