Snap interview question

typical leetcode backtracking question with time and space complexity.