diff --git a/Data-Structures/Trees/Trie/PrefixTrie.java b/Data-Structures/Trees/Trie/PrefixTrie.java index aec9be7..5b5b5f6 100644 --- a/Data-Structures/Trees/Trie/PrefixTrie.java +++ b/Data-Structures/Trees/Trie/PrefixTrie.java @@ -1,43 +1,72 @@ -import java.util.*; -class PrefixNode { - PrefixNode[] children = new PrefixNode[26]; - int count; - PrefixNode() { - count = 0; - } -} -public class PrefixTrie { - PrefixNode root; - public PrefixTrie() { - root = new PrefixNode(); - } - public void insert(String word) { - PrefixNode curr = root; - for (char ch : word.toCharArray()) { - int index = ch - 'a'; - if (curr.children[index] == null) { - curr.children[index] = new PrefixNode(); - } - curr = curr.children[index]; - curr.count++; - } - } - public int countPrefix(String prefix) { - PrefixNode curr = root; - for (char ch : prefix.toCharArray()) { - int index = ch - 'a'; - if (curr.children[index] == null) { - return 0; - } - curr = curr.children[index]; - } - return curr.count; - } - public static void main(String[] args) { - PrefixTrie trie = new PrefixTrie(); - Scanner sc = new Scanner(System.in); - System.out.println("Enter a String: "); - String prefix = sc.nextLine(); - trie.insert(prefix); - } -} \ No newline at end of file +import java.util.*; + +class PrefixNode { + PrefixNode[] children = new PrefixNode[26]; + int count; + + PrefixNode() { + count = 0; + } +} + +public class PrefixTrie { + PrefixNode root; + + public PrefixTrie() { + root = new PrefixNode(); + } + + private int getValidatedIndex(char ch, int position, String input) { + char normalized = Character.toLowerCase(ch); + int index = normalized - 'a'; + if (index < 0 || index >= 26) { + throw new IllegalArgumentException( + "Invalid character '" + ch + "' at position " + position + + " in \"" + input + "\". Please use letters a-z only." + ); + } + return index; + } + + public void insert(String word) { + PrefixNode curr = root; + for (int i = 0; i < word.length(); i++) { + int index = getValidatedIndex(word.charAt(i), i, word); + if (curr.children[index] == null) { + curr.children[index] = new PrefixNode(); + } + curr = curr.children[index]; + curr.count++; + } + } + + public int countPrefix(String prefix) { + PrefixNode curr = root; + for (int i = 0; i < prefix.length(); i++) { + int index = getValidatedIndex(prefix.charAt(i), i, prefix); + if (curr.children[index] == null) { + return 0; + } + curr = curr.children[index]; + } + return curr.count; + } + + public static void main(String[] args) { + PrefixTrie trie = new PrefixTrie(); + + // Valid examples (uppercase is normalized to lowercase) + trie.insert("Apple"); + trie.insert("application"); + trie.insert("app"); + System.out.println("countPrefix(app): " + trie.countPrefix("app")); + System.out.println("countPrefix(AP): " + trie.countPrefix("AP")); + + // Invalid example + try { + trie.insert("ab#c"); + } catch (IllegalArgumentException e) { + System.out.println("Invalid example error: " + e.getMessage()); + } + } +} diff --git a/Data-Structures/Trees/Trie/Trie.java b/Data-Structures/Trees/Trie/Trie.java index b216583..14d7d82 100644 --- a/Data-Structures/Trees/Trie/Trie.java +++ b/Data-Structures/Trees/Trie/Trie.java @@ -1,59 +1,87 @@ -import java.util.*; -class TrieNode { - TrieNode[] children = new TrieNode[26]; - boolean isEndOfWord; - TrieNode() { - isEndOfWord = false; - for (int i = 0; i < 26; i++) { - children[i] = null; - } - } -} -public class Trie { - TrieNode root; - public Trie() { - root = new TrieNode(); - } - public void insert(String word) { - TrieNode curr = root; - for (char ch : word.toCharArray()) { - int index = ch - 'a'; - if (curr.children[index] == null) { - curr.children[index] = new TrieNode(); - } - curr = curr.children[index]; - } - curr.isEndOfWord = true; - } - public boolean search(String word) { - TrieNode curr = root; - for (char ch : word.toCharArray()) { - int index = ch - 'a'; - if (curr.children[index] == null) { - return false; - } - curr = curr.children[index]; - } - return curr.isEndOfWord; - } - public boolean startsWith(String prefix) { - TrieNode curr = root; - for (char ch : prefix.toCharArray()) { - int index = ch - 'a'; - if (curr.children[index] == null) { - return false; - } - curr = curr.children[index]; - } - return true; - } - public static void main(String[] args) { - Trie trie = new Trie(); - trie.insert("apple"); - trie.insert("app"); - System.out.println(trie.search("apple")); - System.out.println(trie.search("app")); - System.out.println(trie.search("appl")); - System.out.println(trie.startsWith("app")); - } -} \ No newline at end of file +import java.util.*; + +class TrieNode { + TrieNode[] children = new TrieNode[26]; + boolean isEndOfWord; + + TrieNode() { + isEndOfWord = false; + for (int i = 0; i < 26; i++) { + children[i] = null; + } + } +} + +public class Trie { + TrieNode root; + + public Trie() { + root = new TrieNode(); + } + + private int getValidatedIndex(char ch, int position, String input) { + char normalized = Character.toLowerCase(ch); + int index = normalized - 'a'; + if (index < 0 || index >= 26) { + throw new IllegalArgumentException( + "Invalid character '" + ch + "' at position " + position + + " in \"" + input + "\". Please use letters a-z only." + ); + } + return index; + } + + public void insert(String word) { + TrieNode curr = root; + for (int i = 0; i < word.length(); i++) { + int index = getValidatedIndex(word.charAt(i), i, word); + if (curr.children[index] == null) { + curr.children[index] = new TrieNode(); + } + curr = curr.children[index]; + } + curr.isEndOfWord = true; + } + + public boolean search(String word) { + TrieNode curr = root; + for (int i = 0; i < word.length(); i++) { + int index = getValidatedIndex(word.charAt(i), i, word); + if (curr.children[index] == null) { + return false; + } + curr = curr.children[index]; + } + return curr.isEndOfWord; + } + + public boolean startsWith(String prefix) { + TrieNode curr = root; + for (int i = 0; i < prefix.length(); i++) { + int index = getValidatedIndex(prefix.charAt(i), i, prefix); + if (curr.children[index] == null) { + return false; + } + curr = curr.children[index]; + } + return true; + } + + public static void main(String[] args) { + Trie trie = new Trie(); + + // Valid examples (uppercase is normalized to lowercase) + trie.insert("Apple"); + trie.insert("app"); + System.out.println("search(apple): " + trie.search("apple")); + System.out.println("search(APP): " + trie.search("APP")); + System.out.println("startsWith(Ap): " + trie.startsWith("Ap")); + + // Invalid example + try { + trie.insert("app3"); + } catch (IllegalArgumentException e) { + System.out.println("Invalid example error: " + e.getMessage()); + } + } +}