Leetcode 856 Python Algorithm