From ebb8f30174d9e701b0d571f5a414b1af62d92549 Mon Sep 17 00:00:00 2001 From: Sandeep Dasika Date: Mon, 29 Mar 2021 20:49:26 +0530 Subject: [PATCH] Minor refactoring --- .../io/github/gravetii/dictionary/Trie.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/github/gravetii/dictionary/Trie.java b/src/main/java/io/github/gravetii/dictionary/Trie.java index 8587b2e..bdfaa7a 100644 --- a/src/main/java/io/github/gravetii/dictionary/Trie.java +++ b/src/main/java/io/github/gravetii/dictionary/Trie.java @@ -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(); @@ -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]; @@ -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];