LeetCode exercise solution