-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicrosoft_Problem_05.java
53 lines (42 loc) · 1.64 KB
/
Microsoft_Problem_05.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
/*
This problem was asked by Microsoft.
Given a 2D matrix of characters and a target word, write a function that returns whether the word can be found in the matrix by going left-to-right, or up-to-down.
For example, given the following matrix:
[['F', 'A', 'C', 'I'],
['O', 'B', 'Q', 'P'],
['A', 'N', 'O', 'B'],
['M', 'A', 'S', 'S']]
and the target word 'FOAM', you should return true, since it's the leftmost column. Similarly, given the target word 'MASS', you should return true, since it's the last row.
*/
import java.util.ArrayList;
import java.util.List;
public class Microsoft_Problem_05 {
public static List<String> getWordsFromMatrix(char[][] matrix) {
List<String> words = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
String horizontal = "";
String vertical = "";
for (int j = 0; j < matrix.length; j++) {
vertical = vertical + matrix[i][j];
horizontal = horizontal + matrix[j][i];
}
words.add(vertical);
words.add(horizontal);
}
return words;
}
public static boolean isExisted(String word, List<String> words) {
return words.contains(word);
}
public static void main(String[] args) {
char[][] matrix = {
{'F', 'A', 'C', 'I'},
{'O', 'B', 'Q', 'P'},
{'A', 'N', 'O', 'B'},
{'M', 'A', 'S', 'S'}
};
assert isExisted("FOAM", getWordsFromMatrix(matrix));
assert isExisted("MASS", getWordsFromMatrix(matrix));
assert !isExisted("DELL", getWordsFromMatrix(matrix));
}
}