Skip to content

Commit 03d2618

Browse files
authored
Merge pull request #5 from Subham1999/back_tracking
Back tracking
2 parents dd0e942 + 6a5000f commit 03d2618

File tree

4 files changed

+186
-34
lines changed

4 files changed

+186
-34
lines changed

.idea/workspace.xml

Lines changed: 53 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<artifactId>rxjava</artifactId>
2828
<version>3.1.8</version>
2929
</dependency>
30+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-api</artifactId>
34+
<version>2.0.16</version>
35+
</dependency>
3036
</dependencies>
3137
<build>
3238
<plugins>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithm.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
/**
8+
* @author: Subham Santra
9+
*/
10+
public class CombinationSumII {
11+
static class Solution {
12+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
13+
List<List<Integer>> result = new ArrayList<>();
14+
15+
Arrays.sort(candidates);
16+
helper(candidates, 0, 0, target, result, new ArrayList<>());
17+
18+
return result;
19+
}
20+
21+
void helper(
22+
final int[] arr,
23+
final int i,
24+
final int sum,
25+
final int target,
26+
final List<List<Integer>> result,
27+
final List<Integer> currentSet) {
28+
29+
if (sum > target) {
30+
return;
31+
}
32+
33+
if (sum == target) {
34+
result.add(new ArrayList<>(currentSet));
35+
return;
36+
}
37+
38+
if (i == arr.length) {
39+
return;
40+
}
41+
42+
// include
43+
currentSet.addLast(arr[i]);
44+
helper(arr, i + 1, sum + arr[i], target, result, currentSet);
45+
currentSet.removeLast();
46+
47+
// exclude : not only the current index also all the indexes having same value
48+
// This is required because we want to remove the duplicate set
49+
// 1 2 2 2 2 2 5
50+
// When we have found {1, 2[1], 2[2]} we shall not add {1, 2[1], 2[3]} ... etc.
51+
int j = i;
52+
while (j < arr.length && arr[j] == arr[i]) ++j;
53+
helper(arr, j, sum, target, result, currentSet);
54+
}
55+
}
56+
57+
public static void main(String[] args) {
58+
System.out.println(new Solution().combinationSum2(new int[] {2, 5, 2, 1, 2}, 5));
59+
System.out.println(new Solution().combinationSum2(new int[] {2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 8, 7, 1, 1}, 8));
60+
}
61+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.thealgorithm.backtracking;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class WordSearch {
7+
public boolean exist(char[][] board, String word) {
8+
final int m = board.length;
9+
final int n = board[0].length;
10+
final boolean[][] taken = new boolean[m][n];
11+
12+
for (int i = 0; i < m; ++i) {
13+
for (int j = 0; j < n; ++j) {
14+
if (board[i][j] == word.charAt(0)) {
15+
if (EXPLORE(board, i, j, m, n, 0, word, taken)) {
16+
return true;
17+
}
18+
}
19+
}
20+
}
21+
22+
return false;
23+
}
24+
25+
boolean EXPLORE(
26+
char[][] board,
27+
int i,
28+
int j,
29+
int m,
30+
int n,
31+
int wordCharIndex,
32+
String word,
33+
boolean[][] taken) {
34+
if (wordCharIndex == word.length()) {
35+
return true;
36+
}
37+
38+
if (!((0 <= i && i < m) && (0 <= j && j < n))) {
39+
return false;
40+
}
41+
42+
if (taken[i][j]) {
43+
return false;
44+
}
45+
46+
boolean ans = false;
47+
48+
taken[i][j] = true;
49+
if (board[i][j] == word.charAt(wordCharIndex)) {
50+
// We will explore further
51+
ans |= EXPLORE(board, i, j + 1, m, n, wordCharIndex + 1, word, taken);
52+
ans |= EXPLORE(board, i, j - 1, m, n, wordCharIndex + 1, word, taken);
53+
ans |= EXPLORE(board, i + 1, j, m, n, wordCharIndex + 1, word, taken);
54+
ans |= EXPLORE(board, i - 1, j, m, n, wordCharIndex + 1, word, taken);
55+
}
56+
taken[i][j] = false;
57+
return ans;
58+
}
59+
60+
public static void main(String[] args) {
61+
char[][] board = {{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
62+
System.out.println(new WordSearch().exist(board, "ABCCED"));
63+
System.out.println(new WordSearch().exist(board, "ABCF"));
64+
System.out.println(new WordSearch().exist(board, "SEE"));
65+
}
66+
}

0 commit comments

Comments
 (0)