Could someone give me some guidance on this? This is my second crack at this and I cant seem to get the last requirement to pass. I've tested with a stub wordsearch in every direction described in the conditions and the solution passes...not sure what im missing...
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> wordsFound = new ArrayList<>();
int rowMax = wordSearch.length - 1;
int colMax = wordSearch[0].length - 1;
//Search for word Horizontal
for (int row = 0; row <= rowMax; row++) {
ArrayList<CharPoint> charList = new ArrayList<>();
for (int col = 0; col <= colMax; col++) {
char currentChar = (char) wordSearch[row][col];
charList.add(new CharPoint(currentChar, col, row));
}
wordsFound = processCharList(charList, wordsFound, words);
}
//Search for word Vertical
for (int col = 0; col <= colMax; col++) {
ArrayList<CharPoint> charList = new ArrayList<>();
for (int row = 0; row <= rowMax; row++) {
char currentChar = (char) wordSearch[row][col];
charList.add(new CharPoint(currentChar, col, row));
}
wordsFound = processCharList(charList, wordsFound, words);
}
//Search for word Top Left to Bottom Right Diagonal : right side
for (int col = 0; col <= colMax; col++) {
ArrayList<CharPoint> charList = new ArrayList<>();
int tmpCol = col;
for (int row = 0; row <= rowMax && tmpCol <= colMax; row++) {
char currentChar = (char) wordSearch[row][tmpCol];
charList.add(new CharPoint(currentChar, tmpCol, row));
tmpCol++;
}
wordsFound = processCharList(charList, wordsFound, words);
}
//Search for word Top Left to Bottom Right Diagonal : left side
for (int row = 1; row <= rowMax; row++) {
ArrayList<CharPoint> charList = new ArrayList<>();
int tmpRow = row;
for (int col = 0; col <= colMax && tmpRow <= rowMax; col++) {
char currentChar = (char) wordSearch[tmpRow][col];
charList.add(new CharPoint(currentChar, col, tmpRow));
tmpRow++;
}
wordsFound = processCharList(charList, wordsFound, words);
}
//Search for word Bottom Left to Top Right Diagonal : right side
for (int col = 0; col <= colMax; col++) {
ArrayList<CharPoint> charList = new ArrayList<>();
int tmpCol = col;
for (int row = rowMax; row >= 0 && tmpCol <= colMax; row--) {
char currentChar = (char) wordSearch[row][tmpCol];
charList.add(new CharPoint(currentChar, tmpCol, row));
tmpCol++;
}
wordsFound = processCharList(charList, wordsFound, words);
}
//Search for word Bottom Left to Top Right Diagonal : left side
for (int row = rowMax - 1; row >= 0; row--) {
ArrayList<CharPoint> charList = new ArrayList<>();
int tmpRow = row;
for (int col = 0; col <= colMax && tmpRow >= 0; col++) {
char currentChar = (char) wordSearch[tmpRow][col];
charList.add(new CharPoint(currentChar, col, tmpRow));
tmpRow--;
}
wordsFound = processCharList(charList, wordsFound, words);
}
// Collections.reverse(wordsFound);
// System.out.println(wordsFound.size());
// for(Word w : wordsFound) {
// System.out.println(w.toString());
// }
return wordsFound;
}
public static List<Word> processCharList(ArrayList<CharPoint> charList, List<Word> wordsFound, String... words) {
//Cloning list then reversing it so we can search forward and backwards through the charList
ArrayList<CharPoint> charListClone = (ArrayList<CharPoint>) charList.clone();
Collections.reverse(charListClone);
Arrays.asList(charList, charListClone).forEach(charPoints -> {
for(String targetWord : words) {
StringBuilder sb = new StringBuilder();
Word maybeWord = new Word(targetWord);
int targetWordToFindIdx = 0;
for(CharPoint charPoint : charPoints) {
//We reset the target index to the beginning of the target word since there
// is not a char to index match
if(charPoint.getCharacter() != targetWord.charAt(targetWordToFindIdx)) {
sb.setLength(0);
targetWordToFindIdx = 0;
}
//We start a char match streak here and increment the target idx as char to index matches
// continue to be found, if that target index is at the beginning of the target word we set the
// start points
if(charPoint.getCharacter() == targetWord.charAt(targetWordToFindIdx)) {
if(targetWordToFindIdx == 0) {
maybeWord.setStartPoint(charPoint.getX(), charPoint.getY());
}
sb.append(charPoint.getCharacter());
targetWordToFindIdx++;
}
//We found a word, so we set the endpoint and add the word to the list of found words
// then we reset the stringbuilder and the target index so we are ready to search next iteration
if(sb.toString().equals(targetWord)) {
maybeWord.setEndPoint(charPoint.getX(), charPoint.getY());
wordsFound.add(maybeWord);
targetWordToFindIdx = 0;
sb.setLength(0);
}
}
}
});
return wordsFound;
}
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);
}
}
//Used to keep track of points and chars in the wordsearch matrix
public static class CharPoint {
private char character;
private int X;
private int Y;
public CharPoint(char character, int x, int y) {
this.character = character;
X = x;
Y = y;
}
public char getCharacter() {
return character;
}
public int getX() {
return X;
}
public int getY() {
return Y;
}
@Override
public String toString() {
return "CharPoint{" +
"character=" + character +
", X=" + X +
", Y=" + Y +
'}';
}
}
}