Pinterest interview question

Leetcode hard question on backtracking.