Amazon Interview Question

Backtracking for both LC style questions