Hello everyone!
Ive tried to solve this with recursion but it fails with message "The list returned by detectAllWords is not the right size!".
System.out.println(words.length) prints 2.
System.out.println(wordsList.size()) prints 2, same size.
System.out.println(wordsList) prints [home - (5, 3) - (2, 0), same - (1, 1) - (4, 1)] and it looks like right answer.
Hope for your help.
package com.codegym.task.task20.task2027;
import java.util.*;
/*
Word search
*/
public class Solution {
public static void main(String[] args) {
int[][] wordSearch = new int[][]{
{'f', 'd', 'e', 'r', 'l', 'k'},
{'u', 's', 'a', 'm', 'e', 'o'},
{'l', 'n', 'g', 'r', 'o', 'v'},
{'m', 'l', 'p', 'r', 'r', 'h'},
{'p', 'o', 'e', 'e', 'j', 'j'}
};
detectAllWords(wordSearch, "home", "same");
/*
Expected result
home - (5, 3) - (2, 0)
same - (1, 1) - (4, 1)
*/
}
public static List<Word> detectAllWords(int[][] wordSearch, String... words) {
List<Word> wordsList = new ArrayList<Word>();
int wsl = wordSearch.length;
int wsll = wordSearch[0].length;
for (String word : words) {
char fchar = word.charAt(0);
for (int x = 0; x < wsl; x++) {
for (int y = 0; y < wsll; y++) {
if (wordSearch[x][y] == fchar) {
// System.out.println("for: "+fchar);
int[] start = {x, y};
int[][] r = checkAround(x, y, 1, word, wordSearch, start, null);
if (r != null) {
// System.out.println("FOR "+word+" RESULT IS: "+r[0][0]+" "+r[0][1]+" - "+r[1][0]+" "+r[1][1]);
Word curWord = new Word(word);
curWord.setStartPoint(r[0][1], r[0][0]);
curWord.setEndPoint(r[1][1], r[1][0]);
wordsList.add(curWord);
}
}
}
}
}
// System.out.println(wordsList);
// System.out.println(words.length);
// System.out.println(wordsList.size());
return wordsList;
}
// x, y - char coordinates; c - word char number; wordSearch - wordSearch;
// start - first char coordinates; dir - direction to search;
private static int[][] checkAround(int x, int y, int c, String word, int[][] wordSearch, int[] start, int[] dir) {
int[] levels = {-1, 0, 1};
int[][] result = null;
if (c == word.length()) { // end of the word
int[] end = {x, y};
result = new int[2][2];
result[0] = start;
result[1] = end;
return result;
} else if (dir == null){ // first dive
for (int i = 0; i < 3; i++) {
for (int k = 0; k < 3; k++) {
try {
int nextX = x + levels[i];
int nextY = y + levels[k];
if ((char) wordSearch[nextX][nextY] == word.charAt(c)) {
dir = new int[]{levels[i], levels[k]};
return checkAround(nextX, nextY, c + 1, word, wordSearch, start, dir);
}
} catch (Exception e) {
continue;
}
}
}
} else { // next char in this direction
try {
int nextX = x + dir[0];
int nextY = y + dir[1];
if ((char) wordSearch[nextX][nextY] == word.charAt(c)) {
return checkAround(nextX, nextY, c + 1, word, wordSearch, start, dir);
}
} catch (Exception e) {
}
}
return null;
}
public static class Word {
private String text;
private int startX;
private int startY;
private int endX;
private int endY;
public Word(String text) {
this.text = text;
}
public void setStartPoint(int i, int j) {
startX = i;
startY = j;
}
public void setEndPoint(int i, int j) {
endX = i;
endY = j;
}
@Override
public String toString() {
return String.format("%s - (%d, %d) - (%d, %d)", text, startX, startY, endX, endY);
}
}
}