Similar Problems

Similar Problems not available

Sum Of Prefix Scores Of Strings - Leetcode Solution

Companies:

LeetCode:  Sum Of Prefix Scores Of Strings Leetcode Solution

Difficulty: Hard

Topics: string array  

The problem "Sum of Prefix Scores of Strings" on LeetCode asks us to calculate the prefix score of each string in the given array of strings and return the sum of all the prefix scores. Here, a prefix score is defined as the sum of scores of all the prefixes of a string.

To solve the problem, we need to first understand the scoring system. Each character in the string is assigned a score based on its position in the English alphabet. For example, 'a' has a score of 1, 'b' has a score of 2, and so on. The score of a string is the sum of scores of all the characters in the string.

Now, to calculate the prefix scores, we can make use of dynamic programming. We can maintain an array to store the prefix scores of all the strings we have encountered till now. For each new string, we can calculate its prefix score by adding its score to the prefix score of the previous string.

The following is the detailed algorithm to solve the problem:

  1. Initialize a variable 'sum' to 0, which will store the sum of all the prefix scores.
  2. Initialize an empty array 'prefixes' to store the prefix scores of all the strings we have encountered till now.
  3. Loop over all the strings in the given array.
  4. Calculate the score of the current string by looping over all its characters and adding their scores.
  5. If the 'prefixes' array is empty, then add the score of the current string to 'sum' and also append the score to 'prefixes'.
  6. If the 'prefixes' array is not empty, then calculate the prefix score of the current string by adding its score to the last element of 'prefixes'.
  7. Add the prefix score of the current string to 'sum' and also append the prefix score to 'prefixes'.
  8. Return 'sum'.

The time complexity of this algorithm is O(NM), where N is the number of strings and M is the maximum length of a string. The space complexity is also O(NM), as we are using the 'prefixes' array to store the prefix scores.

The following is the Python implementation of the above algorithm:

class Solution:
    def sumOfPrefixes(self, arr: List[str]) -> int:
        sum_prefixes = 0
        prefixes = []
        for s in arr:
            score = sum(ord(c) - 96 for c in s)
            if not prefixes:
                sum_prefixes += score
                prefixes.append(score)
            else:
                prefix_score = prefixes[-1] + score
                sum_prefixes += prefix_score
                prefixes.append(prefix_score)
        return sum_prefixes

In this implementation, we are using the 'ord' function to get the ASCII value of a character and subtracting 96 from it to get the character score. We are also using the List type hint to indicate that the 'arr' parameter is a list of strings.

Sum Of Prefix Scores Of Strings Solution Code

1