Class Solution
java.lang.Object
g2201_2300.s2262_total_appeal_of_a_string.Solution
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>"abbca"</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 = “abbca”</p>
<p><strong>Output:</strong> 28</p>
<p><strong>Explanation:</strong> The following are the substrings of “abbca”:</p>
<ul>
<li>
<p>Substrings of length 1: “a”, “b”, “b”, “c”, “a” have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.</p>
</li>
<li>
<p>Substrings of length 2: “ab”, “bb”, “bc”, “ca” have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.</p>
</li>
<li>
<p>Substrings of length 3: “abb”, “bbc”, “bca” have an appeal of 2, 2, and 3 respectively. The sum is 7.</p>
</li>
<li>
<p>Substrings of length 4: “abbc”, “bbca” have an appeal of 3 and 3 respectively. The sum is 6.</p>
</li>
<li>
<p>Substrings of length 5: “abbca” 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 = “code”</p>
<p><strong>Output:</strong> 20</p>
<p><strong>Explanation:</strong> The following are the substrings of “code”:</p>
<ul>
<li>
<p>Substrings of length 1: “c”, “o”, “d”, “e” have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.</p>
</li>
<li>
<p>Substrings of length 2: “co”, “od”, “de” have an appeal of 2, 2, and 2 respectively. The sum is 6.</p>
</li>
<li>
<p>Substrings of length 3: “cod”, “ode” have an appeal of 3 and 3 respectively. The sum is 6.</p>
</li>
<li>
<p>Substrings of length 4: “code” 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 Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
appealSum
-