Skip to content

Commit

Permalink
Avoid second search
Browse files Browse the repository at this point in the history
  • Loading branch information
gravetii committed Mar 29, 2021
1 parent ebb8f30 commit 1ed98be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
7 changes: 2 additions & 5 deletions src/main/java/io/github/gravetii/dictionary/Dictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ public static void main(String[] args) {
Dictionary dictionary = new Dictionary();
System.out.println(dictionary.search("mask"));
System.out.println(dictionary.search("silhouette"));
System.out.println(dictionary.prefix("silhou"));
System.out.println(dictionary.search("silhou"));
System.out.println(dictionary.search("ioklgmm"));
}

public int search(String word) {
return this.trie.search(word);
}

public boolean prefix(String word) {
return this.trie.prefix(word);
}
}
33 changes: 8 additions & 25 deletions src/main/java/io/github/gravetii/dictionary/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class Trie {
}

/**
* Add a new word from the English dictionary to this trie.
* Add a new word to this trie.
*
* @param word the word to add.
* @param word the word to add
*/
public void insert(String word) {
TrieNode node = root;
Expand All @@ -30,38 +30,21 @@ public void insert(String word) {
}

/**
* Find if the given word belongs in the trie by returning its score.
* Search for the given word in the trie and return its score.
*
* @param word the input word.
* @return the score of the word if it's valid, 0 otherwise.
* @param word the input word
* @return the score of the word: > 0 if the word exists, = 0 if the word exists as a prefix, < 0
* if the word doesn't exist
*/
public int search(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
int idx = ch - 'a';
TrieNode[] children = node.getChildren();
if (children[idx] == null) return 0;
if (children[idx] == null) return -1;
node = children[idx];
}

return node != null ? node.getScore() : 0;
}

/**
* Find if the given word is a prefix of some other word in the trie.
*
* @param word the input word.
* @return true if the word is a prefix of some other word, false otherwise.
*/
public boolean prefix(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
int idx = ch - 'a';
TrieNode[] children = node.getChildren();
if (children[idx] == null) return false;
node = children[idx];
}

return node != null;
return node != null ? node.getScore() : -1;
}
}
23 changes: 8 additions & 15 deletions src/main/java/io/github/gravetii/game/GameSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.github.gravetii.dictionary.Dictionary;
import io.github.gravetii.model.GridPoint;
import io.github.gravetii.model.GridUnit;
import io.github.gravetii.util.Utils;

import java.util.Arrays;
import java.util.LinkedList;
Expand All @@ -21,11 +20,6 @@ public GameSolver(GridUnit[][] grid, Dictionary dictionary) {
this.dictionary = dictionary;
}

private int validate(String word) {
if (word.length() < MIN_WORD_LENGTH) return 0;
return this.dictionary.search(word);
}

public GameResult solve() {
for (int i=0;i<4;++i) {
for (int j=0;j<4;++j) {
Expand All @@ -44,15 +38,14 @@ private void crawl(GridPoint point, String prefix, List<GridPoint> seq, boolean[
GridUnit unit = grid[point.x][point.y];
visited[point.x][point.y] = true;
String word = prefix + unit.getLetter();
if (this.dictionary.prefix(word)) {
seq.add(point);
int score = this.validate(word);
if (score > 0) this.result.put(word, score, seq);
for (GridPoint n: point.getNeighbors()) {
if (!visited[n.x][n.y]) {
this.crawl(n, word, new LinkedList<>(seq), visited);
visited[n.x][n.y] = false;
}
int score = this.dictionary.search(word);
if (score < 0) return;
seq.add(point);
if (score > 0 && word.length() >= MIN_WORD_LENGTH) this.result.put(word, score, seq);
for (GridPoint n: point.getNeighbors()) {
if (!visited[n.x][n.y]) {
this.crawl(n, word, new LinkedList<>(seq), visited);
visited[n.x][n.y] = false;
}
}
}
Expand Down

0 comments on commit 1ed98be

Please sign in to comment.