[ALGO] Find the string in 2 dimensional matrix
Given a 2-dim matrix of characters 'm' and a string 's' - find if the string 's' is present in the matrix. Only characters in the neighboring cells of a cell can contribute to the string.
For example, for the case below B,D,F,G are neighbors of X. From 'X' possible strings (or substrings) are XB, XD, XF, XH.
|-----|-----|-----|
| A | B | C |
|-----|-----|-----|
| H | X | D |
|-----|-----|-----|
| G | F | E |
|-----|-----|-----|
Finally, an example. Given the matrix
(0,0)
|-----|-----|-----|-----|
| y | a | a | o |
|-----|-----|-----|-----|
| a | o | a | o |
|-----|-----|-----|-----|
| y | a | o | o |
|-----|-----|-----|-----|
| h | a | a | o |
|-----|-----|-----|-----|
| y | a | h | o |
|-----|-----|-----|-----|
(3,3)
and asked to check for the string 'yahoo' the solution is true :
y - (3,0)
a - (3,1)
h - (3,2)
o - (3,3)
o - (2,3)
For example, for the case below B,D,F,G are neighbors of X. From 'X' possible strings (or substrings) are XB, XD, XF, XH.
|-----|-----|-----|
| A | B | C |
|-----|-----|-----|
| H | X | D |
|-----|-----|-----|
| G | F | E |
|-----|-----|-----|
Finally, an example. Given the matrix
(0,0)
|-----|-----|-----|-----|
| y | a | a | o |
|-----|-----|-----|-----|
| a | o | a | o |
|-----|-----|-----|-----|
| y | a | o | o |
|-----|-----|-----|-----|
| h | a | a | o |
|-----|-----|-----|-----|
| y | a | h | o |
|-----|-----|-----|-----|
(3,3)
and asked to check for the string 'yahoo' the solution is true :
y - (3,0)
a - (3,1)
h - (3,2)
o - (3,3)
o - (2,3)
Subscribe to:
Posts (Atom)