java.lang.Object
g2201_2300.s2262_total_appeal_of_a_string.Solution

public class Solution extends Object
2262 - Total Appeal of A String.<p>Hard</p> <p>The <strong>appeal</strong> of a string is the number of <strong>distinct</strong> characters found in the string.</p> <ul> <li>For example, the appeal of <code>&quot;abbca&quot;</code> is <code>3</code> because it has <code>3</code> distinct characters: <code>'a'</code>, <code>'b'</code>, and <code>'c'</code>.</li> </ul> <p>Given a string <code>s</code>, return <em>the <strong>total appeal of all of its <strong>substrings</strong>.</strong></em></p> <p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;abbca&rdquo;</p> <p><strong>Output:</strong> 28</p> <p><strong>Explanation:</strong> The following are the substrings of &ldquo;abbca&rdquo;:</p> <ul> <li> <p>Substrings of length 1: &ldquo;a&rdquo;, &ldquo;b&rdquo;, &ldquo;b&rdquo;, &ldquo;c&rdquo;, &ldquo;a&rdquo; have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.</p> </li> <li> <p>Substrings of length 2: &ldquo;ab&rdquo;, &ldquo;bb&rdquo;, &ldquo;bc&rdquo;, &ldquo;ca&rdquo; have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.</p> </li> <li> <p>Substrings of length 3: &ldquo;abb&rdquo;, &ldquo;bbc&rdquo;, &ldquo;bca&rdquo; have an appeal of 2, 2, and 3 respectively. The sum is 7.</p> </li> <li> <p>Substrings of length 4: &ldquo;abbc&rdquo;, &ldquo;bbca&rdquo; have an appeal of 3 and 3 respectively. The sum is 6.</p> </li> <li> <p>Substrings of length 5: &ldquo;abbca&rdquo; has an appeal of 3. The sum is 3.</p> </li> </ul> <p>The total sum is 5 + 7 + 7 + 6 + 3 = 28.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;code&rdquo;</p> <p><strong>Output:</strong> 20</p> <p><strong>Explanation:</strong> The following are the substrings of &ldquo;code&rdquo;:</p> <ul> <li> <p>Substrings of length 1: &ldquo;c&rdquo;, &ldquo;o&rdquo;, &ldquo;d&rdquo;, &ldquo;e&rdquo; have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.</p> </li> <li> <p>Substrings of length 2: &ldquo;co&rdquo;, &ldquo;od&rdquo;, &ldquo;de&rdquo; have an appeal of 2, 2, and 2 respectively. The sum is 6.</p> </li> <li> <p>Substrings of length 3: &ldquo;cod&rdquo;, &ldquo;ode&rdquo; have an appeal of 3 and 3 respectively. The sum is 6.</p> </li> <li> <p>Substrings of length 4: &ldquo;code&rdquo; has an appeal of 4. The sum is 4.</p> </li> </ul> <p>The total sum is 4 + 6 + 6 + 4 = 20.</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

    • appealSum

      public long appealSum(String s)