solveing the problem in leetcode.com