💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 100
Intuition\n\n Describe your first thoughts on how to solve this problem. \n- JavaScript Code --> https://leetcode.com/problems/sum-of-prefix-scores-of-strings/s
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/91aaf15e-0d29-4037-b6d4-001a79697047_1727226424.1205616.jpeg)\n<!-- Describe your first thoughts on how to solve this problem. -->\n- ***JavaScript Code -->*** https://leetcode.com/problems/sum-of-prefix-scores-of-strings/submissions/1401331474?envType=daily-question&envId=2024-09-25\n- ***C++ Code -->*** https://leetcode.com/problems/sum-of-prefix-scores-of-strings/submissions/1401320981?envType=daily-question&envId=2024-09-25\n- ***Python3 Code -->*** https://leetcode.com/problems/sum-of-prefix-scores-of-strings/submissions/1401328638?envType=daily-question&envId=2024-09-25\n- ***Java Code -->*** https://leetcode.com/problems/sum-of-prefix-scores-of-strings/submissions/1401327589?envType=daily-question&envId=2024-09-25\n- ***C Code -->*** https://leetcode.com/problems/sum-of-prefix-scores-of-strings/submissions/1401334732?envType=daily-question&envId=2024-09-25\n- ***Python Code -->*** https://leetcode.com/problems/sum-of-prefix-scores-of-strings/submissions/1401328985?envType=daily-question&envId=2024-09-25\n- ***C# Code -->*** https://leetcode.com/problems/sum-of-prefix-scores-of-strings/submissions/1401329426?envType=daily-question&envId=2024-09-25\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(N * M) // Python3 Solution\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(N * M) // Python3 Solution\n# Code\n```java []\nclass Trie {\n    Trie[] children = new Trie[26];\n    int count = 0;\n\n    Trie() {\n    }\n\n    void insert(Trie root, String s) {\n        Trie curr = root;\n        int n = s.length();\n        for (int i = 0; i < n; i++) {\n            int idx = s.charAt(i) - \'a\';\n            if (curr.children[idx] == null)\n                curr.children[idx] = new Trie();\n            curr = curr.children[idx];\n            curr.count++;\n        }\n        return;\n    }\n\n    int search(Trie root, String s) {\n        Trie curr = root;\n        int n = s.length(), current = 0;\n        for (int i = 0; i < n; ++i) {\n            int idx = s.charAt(i) - \'a\';\n            curr = curr.children[idx];\n            if (curr.count == 1)\n                return current + n - i;\n            current += curr.count;\n        }\n        return current;\n    }\n}\n\nclass Solution {\n    public int[] sumPrefixScores(String[] words) {\n        Trie trie = new Trie();\n        int n = words.length, idx = 0;\n        int[] ans = new int[n];\n        Trie root = new Trie();\n        for (int i = 0; i < n; i++)\n            trie.insert(root, words[i]);\n\n        for (int i = 0; i < n; i++)\n            ans[idx++] = trie.search(root, words[i]);\n        return ans;\n    }\n}\n```\n![th.jpeg](https://assets.leetcode.com/users/images/b38afe33-6f4d-47eb-b70f-dbaf085ab1e6_1727226608.7449493.jpeg)\n
['Array', 'String', 'Trie', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']