Problem 3 in leetcode