-
Notifications
You must be signed in to change notification settings - Fork 0
/
23_Aug_2023_Find_String_in_Grid.java
96 lines (79 loc) · 3.7 KB
/
23_Aug_2023_Find_String_in_Grid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Problem Link: https://practice.geeksforgeeks.org/problems/find-the-string-in-grid0111/1
Problem Statement: Given a 2D grid of n*m of characters and a word, find all occurrences of given word in grid.
A word can be matched in all 8 directions at any point.
Word is said to be found in a direction if all characters match in this direction
(not in zig-zag form).
The 8 directions are, horizontally left, horizontally right, vertically up, vertically down, and 4 diagonal directions.
Note: The returning list should be lexicographically smallest.
If the word can be found in multiple directions starting from the same coordinates,
the list should contain the coordinates only once.
Solution Approach:
For each cell where we are able to find the first character of the string, check for all directions
for that cell by using a separate method, if the word is found in any of the directions, add it to the ans list.
At the end put elements of ans list in an array and return the array.
*/
/* ------------CODE---------------- */
class Solution
{
public int[][] searchWord(char[][] grid, String word)
{
// Code here
int m = grid.length;
int n = grid[0].length;
// Only the square matrix will have the largest diagonal - of size of its rows or columns
// Hence we can safely eliminate the case where word length is larger than matrix size
if(word.length()>m && word.length()>n)
return new int[0][0];
List<int[]> ans = new ArrayList<>();
int dir[][] = {{1,1},{-1,-1},{1,0},{-1,0},{0,1},{0,-1},{1,-1},{-1,1}};
// we kind of need to keep going into the same direction for a match
// also we need to know if the remaining length of the matrix is enough for the word to be formed
int index = 0; // index for traversing the string word
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
// check for all directions if current char is equal to matrix[i][j]
if(grid[i][j]==word.charAt(index)) {
boolean isCell = false;
for(int k=0; k<8; k++) {
if(checkDir(dir[k], grid, word, index+1, i, j))
{
isCell = true;
break;
}
}
// if we can get a word from this cell, add it to the answer
if(isCell) {
ans.add(new int[]{i,j});
}
}
}
}
int ret[][] = new int[ans.size()][2];
for(int i=0; i<ret.length; i++) {
ret[i][0] = ans.get(i)[0];
ret[i][1] = ans.get(i)[1];
}
return ret;
}
private boolean checkDir(int[] d, char[][] grid, String word, int index, int r, int c) {
int row = d[0] + r;
int col = d[1] + c;
if(row<0 || col<0 || row>=grid.length || col>=grid[0].length)
return false;
// keep checking for the remaining word in the same direction
while(!(row<0 || col<0 || row>=grid.length || col>=grid[0].length) && index<word.length() && grid[row][col]==word.charAt(index)) {
index++;
row = row + d[0];
col = col + d[1];
}
if(index==word.length())
return true;
else return false;
}
}
/*
Time Complexity: O(m*n) (word.length) - we will keep traversing the while loop of checkDir at most word.length() times
Space Complexity: O(1) - we did not use any additional space, but if we take the ret array into consideration
then O(m*n) space will be taken.
*/