java.lang.Object
g2201_2300.s2223_sum_of_scores_of_built_strings.Solution

public class Solution extends Object
2223 - Sum of Scores of Built Strings.<p>Hard</p> <p>You are <strong>building</strong> a string <code>s</code> of length <code>n</code> <strong>one</strong> character at a time, <strong>prepending</strong> each new character to the <strong>front</strong> of the string. The strings are labeled from <code>1</code> to <code>n</code>, where the string with length <code>i</code> is labeled <code>s<sub>i</sub></code>.</p> <ul> <li>For example, for <code>s = &quot;abaca&quot;</code>, <code>s<sub>1</sub> == &ldquo;a&rdquo;</code>, <code>s<sub>2</sub> == &ldquo;ca&rdquo;</code>, <code>s<sub>3</sub> == &ldquo;aca&rdquo;</code>, etc.</li> </ul> <p>The <strong>score</strong> of <code>s<sub>i</sub></code> is the length of the <strong>longest common prefix</strong> between <code>s<sub>i</sub></code> and <code>s<sub>n</sub></code> (Note that <code>s == s<sub>n</sub></code>).</p> <p>Given the final string <code>s</code>, return <em>the <strong>sum</strong> of the <strong>score</strong> of every</em> <code>s<sub>i</sub></code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;babab&rdquo;</p> <p><strong>Output:</strong> 9</p> <p><strong>Explanation:</strong> For s<sub>1</sub> == &ldquo;b&rdquo;, the longest common prefix is &ldquo;b&rdquo; which has a score of 1.</p> <p>For s<sub>2</sub> == &ldquo;ab&rdquo;, there is no common prefix so the score is 0.</p> <p>For s<sub>3</sub> == &ldquo;bab&rdquo;, the longest common prefix is &ldquo;bab&rdquo; which has a score of 3.</p> <p>For s<sub>4</sub> == &ldquo;abab&rdquo;, there is no common prefix so the score is 0.</p> <p>For s<sub>5</sub> == &ldquo;babab&rdquo;, the longest common prefix is &ldquo;babab&rdquo; which has a score of 5.</p> <p>The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;azbazbzaz&rdquo;</p> <p><strong>Output:</strong> 14</p> <p><strong>Explanation:</strong></p> <p>For s<sub>2</sub> == &ldquo;az&rdquo;, the longest common prefix is &ldquo;az&rdquo; which has a score of 2.</p> <p>For s<sub>6</sub> == &ldquo;azbzaz&rdquo;, the longest common prefix is &ldquo;azb&rdquo; which has a score of 3.</p> <p>For s<sub>9</sub> == &ldquo;azbazbzaz&rdquo;, the longest common prefix is &ldquo;azbazbzaz&rdquo; which has a score of 9.</p> <p>For all other s<sub>i</sub>, the score is 0.</p> <p>The sum of the scores is 2 + 3 + 9 = 14, so we return 14.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10<sup>5</sup></code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • sumScores

      public long sumScores(String s)