Advertisement
nna42799

Untitled

Sep 25th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. from typing import List
  2.  
  3. debug: bool = True
  4.  
  5.  
  6. class Solution:
  7.     def sumPrefixScores(self, words: List[str]) -> List[int]:
  8.         word_count = len(words)
  9.         # sort into dictionary order
  10.         # this is the KEY
  11.         sorted_indices = sorted(range(word_count), key=lambda i: words[i])
  12.         common_prefix_lengths = self._calculate_common_prefix_lengths(
  13.             words, sorted_indices
  14.         )
  15.         scores = self._calculate_scores(words, sorted_indices, common_prefix_lengths)
  16.         return scores
  17.  
  18.     def _calculate_common_prefix_lengths(self, words, sorted_indices):
  19.         common_prefix_lengths = [0] * len(words)
  20.         for i in range(1, len(words)):
  21.             prev_word = words[sorted_indices[i - 1]]
  22.             curr_word = words[sorted_indices[i]]
  23.  
  24.             if debug:
  25.                 print(f"Prev: {prev_word},  Curr: {curr_word}")
  26.  
  27.             common_length = 0
  28.             while (
  29.                 common_length < len(prev_word)
  30.                 and common_length < len(curr_word)
  31.                 and prev_word[common_length] == curr_word[common_length]
  32.             ):
  33.                 common_length += 1
  34.  
  35.             if debug:
  36.                 print(f"CommonPrefixLength[<word: {words[i]}>]={common_length}")
  37.  
  38.             common_prefix_lengths[i] = common_length
  39.         if debug:
  40.             print("Common prefix lengths: ", common_prefix_lengths)
  41.         return common_prefix_lengths
  42.  
  43.     def _calculate_scores(self, words, sorted_indices, common_prefix_lengths):
  44.         scores = [0] * len(words)
  45.         for i, word_index in enumerate(sorted_indices):
  46.             word_length = len(words[word_index])
  47.             scores[word_index] += word_length
  48.             j = i + 1
  49.             common_length = word_length
  50.  
  51.             # bidirectional update
  52.             # - update scores of current word
  53.             # - update scores of all following words, which's score changes caused by this word
  54.             if debug:
  55.                 print(f"Bidirectional update using word: {words[word_index]}")
  56.             while j < len(words):
  57.                 common_length = min(common_length, common_prefix_lengths[j])
  58.                 if common_length == 0:
  59.                     break
  60.                 if debug:
  61.                     print(
  62.                         f"Update word: {words[sorted_indices[j]]}, delta: {common_length}"
  63.                     )
  64.                 scores[word_index] += common_length
  65.                 scores[sorted_indices[j]] += common_length
  66.  
  67.                 j += 1
  68.         return scores
  69.  
  70.  
  71. print(Solution().sumPrefixScores(["abc", "ab", "bc", "b"]))
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement