Link: 1863. Sum of All Subset XOR Totals - Easy
Track: NeetCode150
Question
Restate the problem
Method 1
Method 2
Method - DFS
Approach
- use backtracking to cal every possible solution
- Base case: if reach the end, complete one subset, add
totalinto res - At each index
i, have two choices- pick:
dfs(i+1, total ^ nums[i]) - not pick:
dfs(i+1, total)
- pick:
Complexity
- Time Complexity: O()
- Space Complexity: O(n)
Edge Case
Code
class Solution:
def subsetXORSum(self, nums: List[int]) -> int:
self.ans = 0
n = len(nums)
def dfs(start, total):
if start == n:
self.ans += total
return
dfs(start + 1, total ^ nums[start])
dfs(start + 1, total)
dfs(0, 0)
return self.ansHistory
Jan-21-2026 Peeked
- XOR is
^