java.lang.Object
g2401_2500.s2416_sum_of_prefix_scores_of_strings.Solution

public class Solution extends Object
2416 - Sum of Prefix Scores of Strings.<p>Hard</p> <p>You are given an array <code>words</code> of size <code>n</code> consisting of <strong>non-empty</strong> strings.</p> <p>We define the <strong>score</strong> of a string <code>word</code> as the <strong>number</strong> of strings <code>words[i]</code> such that <code>word</code> is a <strong>prefix</strong> of <code>words[i]</code>.</p> <ul> <li>For example, if <code>words = [&quot;a&quot;, &quot;ab&quot;, &quot;abc&quot;, &quot;cab&quot;]</code>, then the score of <code>&quot;ab&quot;</code> is <code>2</code>, since <code>&quot;ab&quot;</code> is a prefix of both <code>&quot;ab&quot;</code> and <code>&quot;abc&quot;</code>.</li> </ul> <p>Return <em>an array</em> <code>answer</code> <em>of size</em> <code>n</code> <em>where</em> <code>answer[i]</code> <em>is the <strong>sum</strong> of scores of every <strong>non-empty</strong> prefix of</em> <code>words[i]</code>.</p> <p><strong>Note</strong> that a string is considered as a prefix of itself.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;abc&rdquo;,&ldquo;ab&rdquo;,&ldquo;bc&rdquo;,&ldquo;b&rdquo;]</p> <p><strong>Output:</strong> [5,4,3,2]</p> <p><strong>Explanation:</strong> The answer for each string is the following:</p> <ul> <li> <p>&ldquo;abc&rdquo; has 3 prefixes: &ldquo;a&rdquo;, &ldquo;ab&rdquo;, and &ldquo;abc&rdquo;.</p> </li> <li> <p>There are 2 strings with the prefix &ldquo;a&rdquo;, 2 strings with the prefix &ldquo;ab&rdquo;, and 1 string with the prefix &ldquo;abc&rdquo;.</p> </li> </ul> <p>The total is answer[0] = 2 + 2 + 1 = 5.</p> <ul> <li> <p>&ldquo;ab&rdquo; has 2 prefixes: &ldquo;a&rdquo; and &ldquo;ab&rdquo;.</p> </li> <li> <p>There are 2 strings with the prefix &ldquo;a&rdquo;, and 2 strings with the prefix &ldquo;ab&rdquo;.</p> </li> </ul> <p>The total is answer[1] = 2 + 2 = 4.</p> <ul> <li> <p>&ldquo;bc&rdquo; has 2 prefixes: &ldquo;b&rdquo; and &ldquo;bc&rdquo;.</p> </li> <li> <p>There are 2 strings with the prefix &ldquo;b&rdquo;, and 1 string with the prefix &ldquo;bc&rdquo;.</p> </li> </ul> <p>The total is answer[2] = 2 + 1 = 3.</p> <ul> <li> <p>&ldquo;b&rdquo; has 1 prefix: &ldquo;b&rdquo;.</p> </li> <li> <p>There are 2 strings with the prefix &ldquo;b&rdquo;.</p> </li> </ul> <p>The total is answer[3] = 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;abcd&rdquo;]</p> <p><strong>Output:</strong> [4]</p> <p><strong>Explanation:</strong></p> <p>&ldquo;abcd&rdquo; has 4 prefixes: &ldquo;a&rdquo;, &ldquo;ab&rdquo;, &ldquo;abc&rdquo;, and &ldquo;abcd&rdquo;.</p> <p>Each prefix has a score of one, so the total is answer[0] = 1 + 1 + 1 + 1 = 4.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 1000</code></li> <li><code>1 <= words[i].length <= 1000</code></li> <li><code>words[i]</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sumPrefixScores

      public int[] sumPrefixScores(String[] words)