Python solution for leetcode