15% off all plans & roadmaps Use code
See plansLeetCode 90 · Solution & editorial
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
Subsets II is a Backtracking Patterns problem, in the Subsets (Include/Exclude) subpattern. The pattern covers subsets, permutations, combinations, N-Queens, and word search
Generate all subsets using include/exclude decision pattern. Handle duplicates in subsets.
The reference solution runs in O(2^n) time and O(2^n * n) space.
Subsets II has been reported in interviews at Amazon, ByteDance.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Subsets II is a Backtracking Patterns problem, in the Subsets (Include/Exclude) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.