Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 2416: Sum of Prefix Scores of Strings

LeetCode 2416 Solution Explanation

Explanation:

We can solve this problem efficiently by iterating through each word in the array and counting the number of strings that have the current word as a prefix. We can achieve this by creating a Trie data structure where each node represents a character in the word. By traversing the Trie for each word, we can find the count of strings that have the current word as a prefix. Finally, we calculate the sum of scores for all the prefixes of each word.

Algorithm:

  1. Create a Trie data structure to store the words.
  2. Insert all words into the Trie.
  3. Traverse the Trie for each word and count the number of strings with the current word as a prefix.
  4. Calculate the sum of scores for all prefixes of each word.
  5. Return the array of scores.

Time Complexity:

  • Building the Trie: O(n * m) where n is the number of words and m is the average length of a word.
  • Traversing the Trie for each word: O(n * m)
    Overall, the time complexity is O(n * m).

Space Complexity:

  • Trie data structure: O(n * m)
    Overall, the space complexity is O(n * m).

LeetCode 2416 Solutions in Java, C++, Python

class TrieNode {
    TrieNode[] children;
    int count;
    
    public TrieNode() {
        children = new TrieNode[26];
        count = 0;
    }
}

class Solution {
    public int[] prefixScores(String[] words) {
        TrieNode root = new TrieNode();
        int[] scores = new int[words.length];
        
        for (String word : words) {
            TrieNode node = root;
            for (char c : word.toCharArray()) {
                if (node.children[c - 'a'] == null) {
                    node.children[c - 'a'] = new TrieNode();
                }
                node = node.children[c - 'a'];
                node.count++;
            }
        }
        
        for (int i = 0; i < words.length; i++) {
            TrieNode node = root;
            String word = words[i];
            for (char c : word.toCharArray()) {
                node = node.children[c - 'a'];
                scores[i] += node.count;
            }
        }
        
        return scores;
    }
}

Interactive Code Editor for LeetCode 2416

Improve Your LeetCode 2416 Solution

Use the editor below to refine the provided solution for LeetCode 2416. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems