Leetcode Problem 2262. Total Appeal of A String

2262. Total Appeal of A String

Leetcode Solutions

Combinatorics Approach

  1. Initialize a result variable res to 0 and a dictionary prev to store the last occurrence of each character with a default value of -1.
  2. Iterate over the string s with index i and character ch.
  3. For each character, calculate its contribution to the total appeal as (i - prev[ch]) * (n - i).
  4. Update the result res by adding the contribution of the current character.
  5. Update the last occurrence of the current character in prev.
  6. Return the result res.
UML Thumbnail

Dynamic Programming Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...