Skip to content

Latest commit

 

History

History
149 lines (87 loc) · 2.96 KB

File metadata and controls

149 lines (87 loc) · 2.96 KB

Backtracking used for to get all possible solutions.

void backtrack(arguments) {
	if (condition == true) { // Condition when we should stop our exploration.
		result.push_back(current);
		return;
	}
	for (int i = num; i <= last; i++) {
		current.push_back(i); // Choose candidate.
		backtrack(arguments); // Explore more
		current.pop_back();   // Remove candidate.
	}
}

Popular Questions

  1. All Possible Combinations
  2. Subset
  3. Combination Sum
  4. Combination Sum 2
  5. Combinations
  6. Subset2

All Possible Combinations

Input 1:

A = ['ab', 'cd']

Example Output

Output 1:

['ac', 'ad', 'bc', 'bd']

CODE -- ACCORDING TO OUR PATTERN image

SUBSET Example Input

A = [1, 2, 3]

Example Output

[
[],
[1],
[1, 2],
[1, 2, 3],
[1, 3],
[2],
[2, 3],
[3],
]

image

Combination Sum

Example Input

Input 1:

A = [2, 3] B = 2

Input 2:

A = [2, 3, 6, 7] B = 7

Example Output

Output 1:

[ [2] ]

Output 2:

[ [2, 2, 3], [7] ]

image

Combination Sum2

Only diff in combination sum and this problem is duplicate not allowed so we move to next index asap

Input and output

image

image

SubSet2

The only difference between this and the subset problem is, here we store results in set to avoid duplicates while in the set question we used ArrayList to get all subsets including duplicates

Input image

image

Letter Phone

Input

image

Output

Used All Possible Sum Approach

image

Permutation

Input

image

Output

image

N QUEEN

image

Output

image