Skip to content

Added Backtracking pattern #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
149 changes: 149 additions & 0 deletions ✅ Pattern 17: BackTracking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@


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](https://www.interviewbit.com/problems/all-possible-combinations/)
2. [Subset](https://www.interviewbit.com/problems/subset/)
3. [Combination Sum](https://www.interviewbit.com/problems/combination-sum/)
4. [Combination Sum 2](https://www.interviewbit.com/problems/combination-sum-ii/)
5. [Combinations](https://www.interviewbit.com/problems/combinations/)
6. [Subset2](https://www.interviewbit.com/problems/subsets-ii/)

**All Possible Combinations**

Input 1:

```
A = ['ab', 'cd']
```
**Example Output**

Output 1:

```
['ac', 'ad', 'bc', 'bd']
```
CODE -- ACCORDING TO OUR PATTERN
![image](https://github.com/user-attachments/assets/f00d62aa-1a01-4080-9dfb-c872e5e6b663)


**SUBSET**
**Example Input**

A = [1, 2, 3]



**Example Output**

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

![image](https://github.com/user-attachments/assets/96978d2d-5be0-4b1b-b243-507484208465)

**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](https://github.com/user-attachments/assets/8d5d7fdd-b505-4cde-aa50-12084f495ae8)

**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](https://github.com/user-attachments/assets/ca4e43aa-ee52-4837-b970-4bea3e0718cf)

![image](https://github.com/user-attachments/assets/efe1c936-ba27-4522-925b-eec376c6fd59)

**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](https://github.com/user-attachments/assets/6eb69ee9-7807-44e7-a5f9-a6f5e82ca8db)

![image](https://github.com/user-attachments/assets/1c500ee1-1200-4d9a-9d44-2aca157c7701)


**Letter Phone**

Input

![image](https://github.com/user-attachments/assets/8568e54f-6068-4f5f-823d-c1e8a30b3678)

Output

Used All Possible Sum Approach

![image](https://github.com/user-attachments/assets/e2ee6556-1c43-4797-8f61-dad21457162b)

**Permutation**

Input

![image](https://github.com/user-attachments/assets/44adba86-10e0-4adc-9af9-9c1220b3ab85)

Output

![image](https://github.com/user-attachments/assets/2a8d4705-41ad-40c5-8904-991549d2e877)

**N QUEEN**

![image](https://github.com/user-attachments/assets/0fc7b275-9e0b-4a57-84c1-63ae5a1efb62)

Output

![image](https://github.com/user-attachments/assets/d408d73c-520f-40ad-a029-ffbafed4364a)








66 changes: 66 additions & 0 deletions ✅ Pattern 18:Graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
https://leetcode.com/discuss/study-guide/3903992/graph-its-implementation-and-some-popular-bfsdfs-problems

**BFS**

```
import java.util.*;

class Solution {
public List<Integer> bfsOfGraph(int n, List<List<Integer>> adj) {
// Create a visited array to keep track of visited nodes
boolean[] vis = new boolean[n];

// 1. Pick a starting node, push it into the queue, and mark it as visited.
Queue<Integer> q = new LinkedList<>();
vis[0] = true;
q.add(0);
List<Integer> ans = new ArrayList<>();

// 4. Repeat steps 2 and 3.
while (!q.isEmpty()) {

// 2. In every iteration, pop out the 'x' node and put it in the solution list.
int node = q.poll();
ans.add(node);

// 3. All the unvisited adjacent nodes from 'x' are pushed into the queue.
for (int a : adj.get(node)) {
if (!vis[a]) {
vis[a] = true;
q.add(a);
}
}
}
return ans;
}


```

**DFS**

```
class Solution {
public:

void dfs(int node, vector<int>adj[], vector<bool>&vis, vector<int>& ans) {
vis[node] = 1;
ans.push_back(node);

for(auto a:adj[node]) {
if(!vis[a]) {
dfs(a,adj,vis,ans);
}
}
}

// Function to return a list containing the DFS traversal of the graph.
vector<int> dfsOfGraph(int n, vector<int> adj[]) {
vector<bool>vis(n,0);
int s = 0;
vector<int>ans;
dfs(s, adj, vis, ans);
return ans;
}
};
```