Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gravetii committed Mar 29, 2021
1 parent f97cd66 commit ebb8f30
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/main/java/io/github/gravetii/dictionary/Trie.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ public class Trie {
* @param word the word to add.
*/
public void insert(String word) {
char[] arr = word.toCharArray();
TrieNode node = root;
int score = 0;

for (char c : arr) {
int idx = c - 'a';
for (char ch : word.toCharArray()) {
int idx = ch - 'a';
Alphabet alphabet = Alphabet.values()[idx];
TrieNode[] children = node.getChildren();
if (children[idx] == null) children[idx] = new TrieNode();
Expand All @@ -39,10 +37,8 @@ public void insert(String word) {
*/
public int search(String word) {
TrieNode node = root;
char[] arr = word.toCharArray();

for (char c : arr) {
int idx = c - 'a';
for (char ch : word.toCharArray()) {
int idx = ch - 'a';
TrieNode[] children = node.getChildren();
if (children[idx] == null) return 0;
node = children[idx];
Expand All @@ -59,10 +55,8 @@ public int search(String word) {
*/
public boolean prefix(String word) {
TrieNode node = root;
char[] arr = word.toCharArray();

for (char c : arr) {
int idx = c - 'a';
for (char ch : word.toCharArray()) {
int idx = ch - 'a';
TrieNode[] children = node.getChildren();
if (children[idx] == null) return false;
node = children[idx];
Expand Down

0 comments on commit ebb8f30

Please sign in to comment.