Skip to content

Commit e0b5558

Browse files
Merge branch 'master' into master
2 parents bf588eb + 7d57c57 commit e0b5558

40 files changed

+1426
-218
lines changed

pmd-exclude.properties

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,11 @@ com.thealgorithms.maths.Gaussian=UselessParentheses
5353
com.thealgorithms.maths.GcdSolutionWrapper=UselessParentheses
5454
com.thealgorithms.maths.HeronsFormula=UselessParentheses
5555
com.thealgorithms.maths.JugglerSequence=UselessMainMethod
56-
com.thealgorithms.maths.KaprekarNumbers=UselessParentheses
5756
com.thealgorithms.maths.KeithNumber=UselessMainMethod,UselessParentheses
58-
com.thealgorithms.maths.LeonardoNumber=UselessParentheses
5957
com.thealgorithms.maths.LinearDiophantineEquationsSolver=UselessMainMethod,UselessParentheses
6058
com.thealgorithms.maths.MagicSquare=UselessMainMethod
6159
com.thealgorithms.maths.PiNilakantha=UselessMainMethod
6260
com.thealgorithms.maths.Prime.PrimeCheck=UselessMainMethod
63-
com.thealgorithms.maths.PythagoreanTriple=UselessMainMethod
6461
com.thealgorithms.maths.RomanNumeralUtil=UselessParentheses
6562
com.thealgorithms.maths.SecondMinMax=UselessParentheses
6663
com.thealgorithms.maths.SecondMinMaxTest=UnnecessaryFullyQualifiedName
@@ -71,19 +68,16 @@ com.thealgorithms.maths.TrinomialTriangle=UselessMainMethod,UselessParentheses
7168
com.thealgorithms.maths.VectorCrossProduct=UselessMainMethod
7269
com.thealgorithms.maths.Volume=UselessParentheses
7370
com.thealgorithms.matrix.RotateMatrixBy90Degrees=UselessMainMethod
74-
com.thealgorithms.misc.Sparsity=UselessParentheses
7571
com.thealgorithms.others.BankersAlgorithm=UselessMainMethod
7672
com.thealgorithms.others.BrianKernighanAlgorithm=UselessMainMethod
7773
com.thealgorithms.others.CRC16=UselessMainMethod,UselessParentheses
7874
com.thealgorithms.others.CRC32=UselessMainMethod
7975
com.thealgorithms.others.Damm=UnnecessaryFullyQualifiedName,UselessMainMethod
8076
com.thealgorithms.others.Dijkstra=UselessMainMethod
8177
com.thealgorithms.others.GaussLegendre=UselessMainMethod
82-
com.thealgorithms.others.HappyNumbersSeq=UselessMainMethod
8378
com.thealgorithms.others.Huffman=UselessMainMethod
8479
com.thealgorithms.others.InsertDeleteInArray=UselessMainMethod
8580
com.thealgorithms.others.KochSnowflake=UselessMainMethod
86-
com.thealgorithms.others.Krishnamurthy=UselessMainMethod
8781
com.thealgorithms.others.LinearCongruentialGenerator=UselessMainMethod
8882
com.thealgorithms.others.Luhn=UnnecessaryFullyQualifiedName,UselessMainMethod
8983
com.thealgorithms.others.Mandelbrot=UselessMainMethod,UselessParentheses
@@ -94,7 +88,6 @@ com.thealgorithms.others.PerlinNoise=UselessMainMethod,UselessParentheses
9488
com.thealgorithms.others.QueueUsingTwoStacks=UselessParentheses
9589
com.thealgorithms.others.Trieac=UselessMainMethod,UselessParentheses
9690
com.thealgorithms.others.Verhoeff=UnnecessaryFullyQualifiedName,UselessMainMethod
97-
com.thealgorithms.puzzlesandgames.Sudoku=UselessMainMethod
9891
com.thealgorithms.recursion.DiceThrower=UselessMainMethod
9992
com.thealgorithms.searches.HowManyTimesRotated=UselessMainMethod
10093
com.thealgorithms.searches.InterpolationSearch=UselessParentheses
@@ -108,15 +101,11 @@ com.thealgorithms.sorts.MergeSortNoExtraSpace=UselessParentheses
108101
com.thealgorithms.sorts.RadixSort=UselessParentheses
109102
com.thealgorithms.sorts.TreeSort=UselessMainMethod
110103
com.thealgorithms.sorts.WiggleSort=UselessParentheses
111-
com.thealgorithms.stacks.LargestRectangle=UselessMainMethod
112104
com.thealgorithms.stacks.MaximumMinimumWindow=UselessMainMethod
113105
com.thealgorithms.stacks.PostfixToInfix=UselessParentheses
114-
com.thealgorithms.strings.Alphabetical=UselessMainMethod
115106
com.thealgorithms.strings.HorspoolSearch=UnnecessaryFullyQualifiedName,UselessParentheses
116-
com.thealgorithms.strings.KMP=UselessMainMethod
117107
com.thealgorithms.strings.Lower=UselessMainMethod
118108
com.thealgorithms.strings.Palindrome=UselessParentheses
119109
com.thealgorithms.strings.Pangram=UselessMainMethod
120-
com.thealgorithms.strings.RabinKarp=UselessMainMethod
121110
com.thealgorithms.strings.Rotation=UselessMainMethod
122111
com.thealgorithms.strings.Upper=UselessMainMethod

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>org.junit</groupId>
2222
<artifactId>junit-bom</artifactId>
23-
<version>6.0.2</version>
23+
<version>6.0.3</version>
2424
<type>pom</type>
2525
<scope>import</scope>
2626
</dependency>
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>org.mockito</groupId>
4444
<artifactId>mockito-core</artifactId>
45-
<version>5.21.0</version>
45+
<version>5.23.0</version>
4646
<scope>test</scope>
4747
</dependency>
4848
<dependency>
@@ -61,7 +61,7 @@
6161
<plugins>
6262
<plugin>
6363
<artifactId>maven-surefire-plugin</artifactId>
64-
<version>3.5.4</version>
64+
<version>3.5.5</version>
6565
<configuration>
6666
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
6767
</configuration>
@@ -112,7 +112,7 @@
112112
<dependency>
113113
<groupId>com.puppycrawl.tools</groupId>
114114
<artifactId>checkstyle</artifactId>
115-
<version>13.2.0</version>
115+
<version>13.3.0</version>
116116
</dependency>
117117
</dependencies>
118118
</plugin>

spotbugs-exclude.xml

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,9 @@
88
<Match>
99
<Bug pattern="DMI_RANDOM_USED_ONLY_ONCE" />
1010
</Match>
11-
<Match>
12-
<Bug pattern="SF_SWITCH_NO_DEFAULT" />
13-
</Match>
1411
<Match>
1512
<Bug pattern="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT" />
1613
</Match>
17-
<Match>
18-
<Bug pattern="DM_NEXTINT_VIA_NEXTDOUBLE" />
19-
</Match>
20-
<Match>
21-
<Bug pattern="SIC_INNER_SHOULD_BE_STATIC" />
22-
</Match>
2314
<Match>
2415
<Bug pattern="EI_EXPOSE_REP" />
2516
</Match>
@@ -32,15 +23,9 @@
3223
<Match>
3324
<Bug pattern="PA_PUBLIC_PRIMITIVE_ATTRIBUTE" />
3425
</Match>
35-
<Match>
36-
<Bug pattern="MS_PKGPROTECT" />
37-
</Match>
3826
<Match>
3927
<Bug pattern="SE_COMPARATOR_SHOULD_BE_SERIALIZABLE" />
4028
</Match>
41-
<Match>
42-
<Bug pattern="INT_BAD_REM_BY_1" />
43-
</Match>
4429
<Match>
4530
<Bug pattern="FE_FLOATING_POINT_EQUALITY" />
4631
</Match>
@@ -50,15 +35,9 @@
5035
<Match>
5136
<Bug pattern="URF_UNREAD_FIELD" />
5237
</Match>
53-
<Match>
54-
<Bug pattern="RC_REF_COMPARISON" />
55-
</Match>
5638
<Match>
5739
<Bug pattern="MS_EXPOSE_REP" />
5840
</Match>
59-
<Match>
60-
<Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING" />
61-
</Match>
6241
<Match>
6342
<Bug pattern="UWF_UNWRITTEN_FIELD" />
6443
</Match>
@@ -71,9 +50,6 @@
7150
<Match>
7251
<Bug pattern="URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD" />
7352
</Match>
74-
<Match>
75-
<Bug pattern="NP_IMMEDIATE_DEREFERENCE_OF_READLINE" />
76-
</Match>
7753
<Match>
7854
<Bug pattern="EQ_COMPARETO_USE_OBJECT_EQUALS" />
7955
</Match>
@@ -83,9 +59,6 @@
8359
<Match>
8460
<Bug pattern="AT_STALE_THREAD_WRITE_OF_PRIMITIVE" />
8561
</Match>
86-
<Match>
87-
<Bug pattern="SF_SWITCH_FALLTHROUGH" />
88-
</Match>
8962
<!-- fb-contrib -->
9063
<Match>
9164
<Bug pattern="LSC_LITERAL_STRING_COMPARISON" />
@@ -114,12 +87,6 @@
11487
<Match>
11588
<Bug pattern="USBR_UNNECESSARY_STORE_BEFORE_RETURN" />
11689
</Match>
117-
<Match>
118-
<Bug pattern="BL_BURYING_LOGIC" />
119-
</Match>
120-
<Match>
121-
<Bug pattern="UTWR_USE_TRY_WITH_RESOURCES" />
122-
</Match>
12390
<Match>
12491
<Bug pattern="MUI_CONTAINSKEY_BEFORE_GET" />
12592
</Match>
@@ -129,9 +96,6 @@
12996
<Match>
13097
<Bug pattern="UCPM_USE_CHARACTER_PARAMETERIZED_METHOD" />
13198
</Match>
132-
<Match>
133-
<Bug pattern="SUA_SUSPICIOUS_UNINITIALIZED_ARRAY" />
134-
</Match>
13599
<Match>
136100
<Bug pattern="SPP_USE_MATH_CONSTANT" />
137101
</Match>
@@ -150,9 +114,6 @@
150114
<Match>
151115
<Bug pattern="LSYC_LOCAL_SYNCHRONIZED_COLLECTION" />
152116
</Match>
153-
<Match>
154-
<Bug pattern="IOI_USE_OF_FILE_STREAM_CONSTRUCTORS" />
155-
</Match>
156117
<Match>
157118
<Bug pattern="UP_UNUSED_PARAMETER" />
158119
</Match>
@@ -189,24 +150,15 @@
189150
<Match>
190151
<Bug pattern="STT_STRING_PARSING_A_FIELD" />
191152
</Match>
192-
<Match>
193-
<Bug pattern="IMC_IMMATURE_CLASS_BAD_SERIALVERSIONUID" />
194-
</Match>
195153
<Match>
196154
<Bug pattern="DRE_DECLARED_RUNTIME_EXCEPTION" />
197155
</Match>
198-
<Match>
199-
<Bug pattern="BAS_BLOATED_ASSIGNMENT_SCOPE" />
200-
</Match>
201156
<Match>
202157
<Bug pattern="SPP_FIELD_COULD_BE_STATIC" />
203158
</Match>
204159
<Match>
205160
<Bug pattern="ITC_INHERITANCE_TYPE_CHECKING" />
206161
</Match>
207-
<Match>
208-
<Bug pattern="FII_USE_ARRAYS_STREAM" />
209-
</Match>
210162
<Match>
211163
<Bug pattern="UTAO_JUNIT_ASSERTION_ODDITIES_USE_ASSERT_EQUALS" />
212164
</Match>

src/main/java/com/thealgorithms/backtracking/WordSearch.java

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,51 +35,34 @@
3535
* - Stack space for the recursive DFS function, where L is the maximum depth of recursion (length of the word).
3636
*/
3737
public class WordSearch {
38-
private final int[] dx = {0, 0, 1, -1};
39-
private final int[] dy = {1, -1, 0, 0};
40-
private boolean[][] visited;
41-
private char[][] board;
42-
private String word;
43-
44-
/**
45-
* Checks if the given (x, y) coordinates are valid positions in the board.
46-
*
47-
* @param x The row index.
48-
* @param y The column index.
49-
* @return True if the coordinates are within the bounds of the board; false otherwise.
50-
*/
51-
private boolean isValid(int x, int y) {
52-
return x >= 0 && x < board.length && y >= 0 && y < board[0].length;
53-
}
5438

5539
/**
5640
* Performs Depth First Search (DFS) from the cell (x, y)
5741
* to search for the next character in the word.
5842
*
5943
* @param x The current row index.
6044
* @param y The current column index.
61-
* @param nextIdx The index of the next character in the word to be matched.
45+
* @param idx The index of the next character in the word to be matched.
6246
* @return True if a valid path is found to match the remaining characters of the word; false otherwise.
6347
*/
64-
private boolean doDFS(int x, int y, int nextIdx) {
65-
visited[x][y] = true;
66-
if (nextIdx == word.length()) {
48+
49+
private boolean dfs(char[][] board, int x, int y, String word, int idx) {
50+
if (idx == word.length()) {
6751
return true;
6852
}
6953

70-
for (int i = 0; i < 4; ++i) {
71-
int xi = x + dx[i];
72-
int yi = y + dy[i];
73-
if (isValid(xi, yi) && board[xi][yi] == word.charAt(nextIdx) && !visited[xi][yi]) {
74-
boolean exists = doDFS(xi, yi, nextIdx + 1);
75-
if (exists) {
76-
return true;
77-
}
78-
}
54+
if (x < 0 || y < 0 || x >= board.length || y >= board[0].length || board[x][y] != word.charAt(idx)) {
55+
return false;
7956
}
8057

81-
visited[x][y] = false; // Backtrack
82-
return false;
58+
char temp = board[x][y];
59+
board[x][y] = '#';
60+
61+
boolean found = dfs(board, x + 1, y, word, idx + 1) || dfs(board, x - 1, y, word, idx + 1) || dfs(board, x, y + 1, word, idx + 1) || dfs(board, x, y - 1, word, idx + 1);
62+
63+
board[x][y] = temp;
64+
65+
return found;
8366
}
8467

8568
/**
@@ -90,20 +73,21 @@ private boolean doDFS(int x, int y, int nextIdx) {
9073
* @param word The target word to search for in the board.
9174
* @return True if the word exists in the board; false otherwise.
9275
*/
76+
9377
public boolean exist(char[][] board, String word) {
94-
this.board = board;
95-
this.word = word;
96-
for (int i = 0; i < board.length; ++i) {
97-
for (int j = 0; j < board[0].length; ++j) {
98-
if (board[i][j] == word.charAt(0)) {
99-
visited = new boolean[board.length][board[0].length];
100-
boolean exists = doDFS(i, j, 1);
101-
if (exists) {
102-
return true;
103-
}
78+
79+
int m = board.length;
80+
int n = board[0].length;
81+
82+
// DFS search
83+
for (int i = 0; i < m; i++) {
84+
for (int j = 0; j < n; j++) {
85+
if (board[i][j] == word.charAt(0) && dfs(board, i, j, word, 0)) {
86+
return true;
10487
}
10588
}
10689
}
90+
10791
return false;
10892
}
10993
}

0 commit comments

Comments
 (0)