This code can find a word if it goes from LEFT to RIGHT, how can I change it to find a word from Right to left and from top to bottom?
boolean findWE(String word) {
for (int r = 0; r < height; r++) {
for (int c = 0; c <= width - word.length(); c++) {
if (word.charAt(0) == grid[r][c]) {
boolean failed = false;
for (int i = 0; i < word.length() && !failed; i++) {
if (word.charAt(i) != grid[r][c + i]) {
failed = true;
}
}
if (!failed) {
System.out.printf("%s found WE at(%d,%d)\n", word, r, c);
return true;
}
}
}
}
return false;
}
Since this isn’t homework, you should just be able to use the library functions to reverse a String:
Or, since finding “abc” in the “edcba” backwards is the same as finding “cba” in “edcba” forwards, you can do the following: