I've got the right answer, but still not pass verification.
package com.codegym.task.task20.task2027;
import java.util.ArrayList;
import java.util.List;
/*
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) {
ArrayList<Word> res = new ArrayList<>();
for(String word : words) {
Word w = new Word(word);
for (int y = 0; y < wordSearch.length; y++) {
for (int x = 0; x < wordSearch[y].length; x++) {
if ((char) wordSearch[y][x] == word.charAt(0)) {
w.setStartPoint(x, y);
}
}
}
ArrayList<String> list = getWord(wordSearch, w.startY, w.startX, word.length(), word);
for (String string : list) {
int[] i = direction(string, w.startY, w.startX, word.length());
w.setEndPoint(i[1], i[0]);
}
res.add(w);
}
return res;
}
public static ArrayList<String> getWord(int[][] wordSearch, int first, int second, int wLength, String str){
ArrayList<String> list = new ArrayList<>();
try{
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for(int y = f; y > f - wLength; y--){
sb.append((char)wordSearch[y][s]);
s--;
}
if(sb.toString().equals(str)){
list.add(str + " diagUpLeft");
}
}catch(IndexOutOfBoundsException e){}
try{
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for(int y = f; y > f - wLength; y--){
sb.append((char)wordSearch[y][s]);
}
if(sb.toString().equals(str)){
list.add(str + " verUp");
}
}catch(IndexOutOfBoundsException e){}
try {
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for (int y = f; y > f - wLength; y--) {
sb.append((char) wordSearch[y][s]);
s++;
}
if (sb.toString().equals(str)) {
list.add(str + " diagUpRight");
}
}catch(IndexOutOfBoundsException e){}
try{
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for(int x = s; x > s - wLength; x--){
sb.append((char)wordSearch[f][x]);
}
if(sb.toString().equals(str)){
list.add(str + " horLeft");
}
}catch(IndexOutOfBoundsException e){}
try{
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for(int x = s; x < s + wLength; x++){
sb.append((char)wordSearch[f][x]);
}
if(sb.toString().equals(str)){
list.add(str + " horRight");
}
}catch(IndexOutOfBoundsException e){}
try{
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for(int y = f; y < f + wLength; y++){
sb.append((char)wordSearch[y][s]);
s--;
}
if(sb.toString().equals(str)){
list.add(str + " diagDownLeft");
}
}catch(IndexOutOfBoundsException e){}
try{
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for(int y = f; y < f + wLength; y++){
sb.append((char)wordSearch[y][s]);
}
if(sb.toString().equals(str)){
list.add(str + " verDown");
}
}catch(IndexOutOfBoundsException e){}
try{
StringBuilder sb = new StringBuilder();
int f = first;
int s = second;
for(int y = f; y < f + wLength; y++){
sb.append((char)wordSearch[y][s]);
s++;
}
if(sb.toString().equals(str)){
list.add(str + " diagDownRight");
}
}catch(IndexOutOfBoundsException e){}
return list;
}
public static int[] direction(String s, int y, int x, int lw){
String[] str = s.split(" ");
int[] res = new int[2];
int l = lw-1;
if (str[1].equals("diagUpLeft")){
res[0] = y-l;
res[1] = x-l;
}
else if(str[1].equals("verUp")){
res[0] = y-l;
res[1] = x;
}
else if(str[1].equals("diagUpRight")){
res[0] = y-l;
res[1] = x+l;
}
else if(str[1].equals("horLeft")){
res[0] = y;
res[1] = x-l;
}
else if(str[1].equals("horRight")){
res[0] = y;
res[1] = x+l;
}
else if(str[1].equals("diagDownLeft")){
res[0] = y+l;
res[1] = x-l;
}
else if(str[1].equals("verDown")){
res[0] = y+l;
res[1] = x;
}
else if(str[1].equals("diagDownRight")){
res[0] = y+l;
res[1] = x+l;
}
return res;
}
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);
}
}
}