Possible Duplicate:
How to find list of possible words from a letter matrix [Boggle Solver]
I have a String[][]
array such as
h,b,c,d
e,e,g,h
i,l,k,l
m,l,o,p
I need to match an ArrayList against this array to find the words specified in the ArrayList. When searching for word hello
, I need to get a positive match and the locations of the letters, for example in this case (0,0)
, (1,1)
, (2,1)
, (3,1)
and (3,2)
.
When going letter by letter and we suppose we are successfully located the first l
letter, the program should try to find the next letter (l
) in the places next to it. So it should match against e, e, g, k, o, l, m and i meaning all the letters around it: horizontally, vertically and diagonally. The same position can't be found in the word twice, so (0,0)
, (1,1)
, (2,1)
, (2,1)
and (3,2)
wouldn't be acceptable because the position (2,1)
is matched twice. In this case, both will match the word because diagonally location is allowed but it needs to match the another l
due to the requirement that a position can not be used more than once.
This case should also be matched
h,b,c,d
e,e,g,h
l,l,k,l
m,o,f,p
If we suppose that we try to search for helllo
, it won't match. Either (x1, y1) (x1, y1)
or (x1, y1) (x2, y2) (x1, y1)
can't be matched.
What I want to know what is the best way to implement this kind of feature. If I have 4x4 String[][]
array and 100 000 words in an ArrayList, what is the most efficient and the easiest way to do this?