1+ /*
2+ * Algorithm Name: Word Search
3+ * Programming Language: Java
4+ * Category: Matrix, Backtracking, Depth-First Search (DFS)
5+ * Difficulty Level: Hard
6+ *
7+ * Author: Ojasvi Bakshi
8+ *
9+ * Algorithm Description:
10+ * Given an m x n grid of characters and a string "word", this algorithm returns true
11+ * if the word exists in the grid. The word can be constructed from letters of sequentially
12+ * adjacent cells, where "adjacent" cells are horizontally or vertically neighboring.
13+ * The same letter cell may not be used more than once.
14+ *
15+ * Time Complexity: O(N * 3^L), where N is the number of cells and L is the length
16+ * of the word. For each cell, we branch out, but we don't go back, so there are 3 choices.
17+ * Space Complexity: O(L) for the recursion stack depth.
18+ */
19+ package DSA_Code .Java .algorithms .matrix ;
20+
21+ import java .util .Scanner ;
22+
23+ public class WordSearch {
24+
25+ public static boolean exist (char [][] board , String word ) {
26+ if (board == null || board .length == 0 || word == null || word .length () == 0 ) {
27+ return false ;
28+ }
29+
30+ int m = board .length ;
31+ int n = board [0 ].length ;
32+
33+ for (int i = 0 ; i < m ; i ++) {
34+ for (int j = 0 ; j < n ; j ++) {
35+ // Start the search only if the first character matches
36+ if (board [i ][j ] == word .charAt (0 )) {
37+ if (search (board , word , i , j , 0 )) {
38+ return true ;
39+ }
40+ }
41+ }
42+ }
43+ return false ;
44+ }
45+
46+ private static boolean search (char [][] board , String word , int r , int c , int index ) {
47+ // Base Case 1: Success - we've found all characters in the word
48+ if (index == word .length ()) {
49+ return true ;
50+ }
51+
52+ // Base Case 2: Failure - out of bounds or character mismatch
53+ if (r < 0 || c < 0 || r >= board .length || c >= board [0 ].length || board [r ][c ] != word .charAt (index )) {
54+ return false ;
55+ }
56+
57+ // Mark the current cell as visited to avoid reusing it
58+ char temp = board [r ][c ];
59+ board [r ][c ] = '#' ;
60+
61+ // Explore neighbors (up, down, left, right)
62+ boolean found = search (board , word , r + 1 , c , index + 1 ) ||
63+ search (board , word , r - 1 , c , index + 1 ) ||
64+ search (board , word , r , c + 1 , index + 1 ) ||
65+ search (board , word , r , c - 1 , index + 1 );
66+
67+ // Backtrack: un-mark the cell, restoring it for other paths
68+ board [r ][c ] = temp ;
69+
70+ return found ;
71+ }
72+
73+
74+ public static void main (String [] args ) {
75+ Scanner sc = new Scanner (System .in );
76+
77+ System .out .print ("Enter number of rows: " );
78+ int m = sc .nextInt ();
79+ System .out .print ("Enter number of columns: " );
80+ int n = sc .nextInt ();
81+
82+ char [][] board = new char [m ][n ];
83+
84+ System .out .println ("Enter grid characters:" );
85+ for (int i = 0 ; i < m ; i ++) {
86+ for (int j = 0 ; j < n ; j ++) {
87+ board [i ][j ] = sc .next ().charAt (0 );
88+ }
89+ }
90+
91+ System .out .print ("\n Enter the word to search for: " );
92+ String word = sc .next ();
93+
94+ System .out .println ("\n Original Grid:" );
95+ for (char [] row : board ) {
96+ for (char cell : row ) {
97+ System .out .print (cell + " " );
98+ }
99+ System .out .println ();
100+ }
101+
102+ // Apply the algorithm
103+ boolean found = exist (board , word );
104+
105+ if (found ) {
106+ System .out .println ("\n The word \" " + word + "\" was found in the grid. ✅" );
107+ } else {
108+ System .out .println ("\n The word \" " + word + "\" was not found in the grid. ❌" );
109+ }
110+
111+ sc .close ();
112+ }
113+ }
0 commit comments