Skip to content

Instantly share code, notes, and snippets.

@ketankhairnar
Created December 28, 2017 05:10
Show Gist options
  • Save ketankhairnar/de82f7295b7e134dc81e9aca20128d00 to your computer and use it in GitHub Desktop.
Save ketankhairnar/de82f7295b7e134dc81e9aca20128d00 to your computer and use it in GitHub Desktop.
HortonGrid
package com.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class GridQuestion {
public static void main(String[] args) {
// [
// ['A','B','C','E'],
// ['S','F','C','S'],
// ['A','D','E','E']
// ]
char[][] boardData = { { 'A', 'B', 'C', 'E' }, { 'S', 'F', 'C', 'S' }, { 'A', 'D', 'E', 'E' } };
System.out.println(parseBoard(boardData));
// word = "ABCCED", -> returns true,
// word = "SEE", -> returns true,
// word = "ABCB", -> returns false.
System.out.println(isWordInBoard(boardData, "ABCCED"));
System.out.println(isWordInBoard(boardData, "SEE"));
System.out.println(isWordInBoard(boardData, "ABCB"));
System.out.println(isWordInBoard(boardData, "SFBA"));
}
public static boolean isWordInBoard(char[][] boardData, String word) {
Map<Character, List<Set<Character>>> result = parseBoard(boardData);
boolean isExists = true;
char[] chars = word.toCharArray();
int charCount = 0;
while (charCount < chars.length - 1) {
Character currChar = chars[charCount];
Character nextChar = chars[charCount + 1];
Set<Character> searchSet = new HashSet<Character>();
searchSet.addAll(result.get(currChar).get(0));
searchSet.addAll(result.get(currChar).get(1));
System.out.println(currChar + "," + nextChar + "," + searchSet);
if (!result.keySet().contains(currChar)) {
isExists = false;
break;
}
if (!searchSet.contains(nextChar)) {
isExists = false;
break;
}
charCount++;
}
return isExists;
}
private static Map<Character, List<Set<Character>>> parseBoard(char[][] boardData) {
int cols = boardData[0].length;
int rows = boardData.length;
Map<Character, List<Set<Character>>> result = new HashMap<Character, List<Set<Character>>>();
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Set<Character> prev = new HashSet<Character>();
Set<Character> next = new HashSet<Character>();
List<Set<Character>> mapEntry = new ArrayList<Set<Character>>();
Character currentChar = boardData[row][col];
if (row - 1 >= 0) {
prev.add(boardData[row - 1][col]);
}
if (col - 1 >= 0) {
prev.add(boardData[row][col - 1]);
}
if (row + 1 < rows) {
next.add(boardData[row + 1][col]);
}
if (col + 1 < cols) {
next.add(boardData[row][col + 1]);
}
// assumption 0th elem in list is prev and 1st elem is for next
// items
mapEntry.add(prev);
mapEntry.add(next);
if (result.keySet().contains(currentChar)) {
result.get(currentChar).get(0).addAll(prev);
result.get(currentChar).get(1).addAll(next);
} else {
result.put(currentChar, mapEntry);
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment