[Java] Trie | O(NM) Time | Easy to Understand
java\nclass Solution {\n    public int[] sumPrefixScores(String[] words) {\n        if (words.length == 1) {\n            return new int[] { words[0].length() }
``` java\nclass Solution {\n    public int[] sumPrefixScores(String[] words) {\n        if (words.length == 1) {\n            return new int[] { words[0].length() };\n        }\n        \n        Trie trie = new Trie();\n        for (String word : words) {\n            trie.addWord(word);\n        }\n        int[] ans = new int[words.length];\n        for (int i = 0; i < words.length; i++) {\n            ans[i] = trie.getPrefixCount(words[i]);\n        }\n        return ans;\n    }\n}\n\nclass Trie {\n    TrieNode root;\n    \n    Trie() {\n        root = new TrieNode();\n    }\n    \n    void addWord(String word) {\n        TrieNode current = root;\n        for (char c : word.toCharArray()) {\n            int index = c - \'a\';\n            if (current.children[index] == null) {\n                current.children[index] = new TrieNode();\n            }\n            current.children[index].prefixCount++;\n            current = current.children[index];\n        }\n    }\n    \n    int getPrefixCount(String word) {\n        int sum = 0;\n        TrieNode current = root;\n        for (char c : word.toCharArray()) {\n            int index = c - \'a\';\n            if (current.children[index] == null) {\n                return sum;\n            }\n            current = current.children[index];\n            sum += current.prefixCount;\n        }\n        return sum;\n    }\n}\n\nclass TrieNode {\n    final static int R = 26;\n    \n    int prefixCount;\n    TrieNode[] children;\n    \n    TrieNode() {\n        prefixCount = 0;\n        children = new TrieNode[R];\n    }\n}\n```
['Trie', 'Java']