Skip to content

Commit 4407796

Browse files
paramagparamag
authored andcommitted
word search problem.
1 parent e41c1d3 commit 4407796

File tree

4 files changed

+155
-31
lines changed

4 files changed

+155
-31
lines changed

Algorithm/.vs/Algorithms/v14/.suo

1.5 KB
Binary file not shown.

Algorithm/LeetCode/Algorithms.Problems.LeetCode.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="LinkedList\MergeTwoLists.cs" />
5454
<Compile Include="LinkedList\RemoveNthNodeFromList.cs" />
5555
<Compile Include="Matrix\RotationMatrix.cs" />
56+
<Compile Include="Matrix\WordSearchProblem.cs" />
5657
<Compile Include="MeetingRoom\Meeting.cs" />
5758
<Compile Include="MeetingRoom\MeetingRoomProblems.cs" />
5859
<Compile Include="PlayGround.cs" />

Algorithm/LeetCode/Arrays/SearchInArrays.cs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
8-
namespace LeetCode.Arrays
1+
namespace LeetCode.Arrays
92
{
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
1010
[TestClass]
1111
public class SearchInArrays
1212
{
13+
public int SearchInRotatedSortedArray_Improved(int[] array, int target)
14+
{
15+
return -1;
16+
}
17+
1318
/// <summary>
1419
/// Rotated sorted array.
1520
/// In Rotated sorted array, there is a point where a value is less than previous value.
@@ -99,30 +104,7 @@ private int FindPivot(int[] array)
99104
}
100105

101106
public void SearchForRangeInArray()
102-
{ }
103-
104-
/// <summary>
105-
/// Not good impl as it's o(n).
106-
/// </summary>
107-
/// <param name="array"></param>
108-
/// <param name="target"></param>
109-
/// <returns></returns>
110-
public int SearchInsertPositionInArray(int[] array, int target)
111107
{
112-
int counter = 0;
113-
while (counter < array.Length)
114-
{
115-
if (array[counter] < target)
116-
{
117-
counter += 1;
118-
}
119-
else if (array[counter] == target || array[counter] > target)
120-
{
121-
return counter;
122-
}
123-
}
124-
125-
return counter;
126108
}
127109

128110
/// <summary>
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace LeetCode.Matrix
10+
{
11+
[TestClass]
12+
public class WordSearchProblem
13+
{
14+
private int[] rowDir = { -1, -1, -1, 0, 0, 1, 1, 1};
15+
private int[] colDir = { -1, 0, 1, -1, 1, -1, 0, 1 };
16+
17+
public bool WordSearchHelper(char[,] matrix, string word, int row, int col)
18+
{
19+
if (matrix[row, col] != word[0])
20+
return false;
21+
22+
for(int i=0; i < 8; i++)
23+
{
24+
int rowd = row + rowDir[i];
25+
int cold = col + colDir[i];
26+
int k = 1;
27+
28+
for (; k < word.Length; k++)
29+
{
30+
// checking for boundaries.
31+
if (rowd < 0 || cold < 0 || rowd >= matrix.GetLength(0) || cold >= matrix.GetLength(1))
32+
{
33+
break;
34+
}
35+
36+
if (matrix[rowd,cold] != word[k])
37+
{
38+
break;
39+
}
40+
41+
// This is important, to make it go in specific direction (top or bottom or left or right or diagonal)
42+
rowd += rowDir[i];
43+
cold += colDir[i];
44+
}
45+
46+
if ( k == word.Length)
47+
{
48+
return true;
49+
}
50+
}
51+
52+
return false;
53+
}
54+
55+
public void WordSearchSpecifcDirection(char[,] matrix, string word)
56+
{
57+
for( int i=0; i < matrix.GetLength(0); i++)
58+
{
59+
for (int j=0; j < matrix.GetLength(1); j++)
60+
{
61+
if (this.WordSearchHelper(matrix, word, i, j))
62+
{
63+
Debug.WriteLine(string.Format("The word {0}: appeared at the index row: {1} col:{2}", word, i, j));
64+
}
65+
}
66+
}
67+
}
68+
69+
public bool WordSearchZigZagHelper(char[,] matrix, string word, int row, int col, bool[,] visitedArray, int wordCount, string wordStr)
70+
{
71+
if (wordStr == word)
72+
{
73+
Debug.WriteLine(word);
74+
return true;
75+
}
76+
77+
visitedArray[row, col] = true;
78+
79+
for (int i = 0; i < 8; i++)
80+
{
81+
int rowd = row + rowDir[i];
82+
int cold = col + colDir[i];
83+
84+
if (!(rowd < 0 || cold < 0 || rowd >= matrix.GetLength(0) || cold >= matrix.GetLength(1)) // check for boundaries.
85+
&& matrix[rowd, cold] == word[wordCount]
86+
&& !visitedArray[rowd, cold])
87+
{
88+
return this.WordSearchZigZagHelper(matrix, word, rowd, cold, visitedArray, wordCount + 1, wordStr + matrix[rowd, cold]);
89+
}
90+
}
91+
92+
visitedArray[row, col] = false;
93+
94+
return false;
95+
}
96+
97+
public bool Exist(char[,] board, string word)
98+
{
99+
bool[,] visitedArray = new bool[board.GetLength(0), board.GetLength(1)];
100+
for(int i=0; i< board.GetLength(0); i++)
101+
{
102+
for (int j=0; j< board.GetLength(1); j++)
103+
{
104+
if (board[i,j] == word[0])
105+
{
106+
bool result = this.WordSearchZigZagHelper(board, word, i, j, visitedArray, 1, word[0].ToString());
107+
108+
if (result) return true;
109+
}
110+
}
111+
}
112+
113+
return false;
114+
}
115+
116+
[TestMethod]
117+
public void TestWordSearch()
118+
{
119+
char[,] matrix = { { 'c', 'a', 't', 'c', 'a', 't'},
120+
{ 'a', 'a', 'a', 'o', 'o', 's'},
121+
{ 'p', 'a', 't', 'w', 'a', 's'}
122+
};
123+
124+
// We can find words like cat, cap, tap, pat, was, tow, cow
125+
// We should also find the repetition, but dont go in zig-zag only 8 direction
126+
// (left, right, top, bottom, diagonal (rightdown), (rightup), (leftup), (leftdown)
127+
128+
this.WordSearchSpecifcDirection(matrix, "tac");
129+
}
130+
131+
[TestMethod]
132+
public void TestWordSearchZigZag()
133+
{
134+
char[,] matrix = { { 'A', 'B', 'C', 'S'}, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'F' } };
135+
136+
Assert.IsTrue(this.Exist(matrix, "ABCCED"));
137+
Assert.IsTrue(this.Exist(matrix, "SFE"));
138+
Assert.IsFalse(this.Exist(matrix, "ABCB"));
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)