Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
The appeal of a string is the number of distinct characters found in the string.
"abbca" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'.Given a string s, return the total appeal of all of its substrings.
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "abbca" Output: 28 Explanation: The following are the substrings of "abbca": - Substrings of length 1: "a", "b", "b", "c", "a" have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5. - Substrings of length 2: "ab", "bb", "bc", "ca" have an appeal of 2, 1, 2, and 2 respectively. The sum is 7. - Substrings of length 3: "abb", "bbc", "bca" have an appeal of 2, 2, and 3 respectively. The sum is 7. - Substrings of length 4: "abbc", "bbca" have an appeal of 3 and 3 respectively. The sum is 6. - Substrings of length 5: "abbca" has an appeal of 3. The sum is 3. The total sum is 5 + 7 + 7 + 6 + 3 = 28.
Example 2:
Input: s = "code" Output: 20 Explanation: The following are the substrings of "code": - Substrings of length 1: "c", "o", "d", "e" have an appeal of 1, 1, 1, and 1 respectively. The sum is 4. - Substrings of length 2: "co", "od", "de" have an appeal of 2, 2, and 2 respectively. The sum is 6. - Substrings of length 3: "cod", "ode" have an appeal of 3 and 3 respectively. The sum is 6. - Substrings of length 4: "code" has an appeal of 4. The sum is 4. The total sum is 4 + 6 + 6 + 4 = 20.
Constraints:
1 <= s.length <= 105s consists of lowercase English letters.Problem summary: The appeal of a string is the number of distinct characters found in the string. For example, the appeal of "abbca" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'. Given a string s, return the total appeal of all of its substrings. A substring is a contiguous sequence of characters within a string.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Dynamic Programming
"abbca"
"code"
count-unique-characters-of-all-substrings-of-a-given-string)count-vowel-substrings-of-a-string)vowels-of-all-substrings)find-the-median-of-the-uniqueness-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2262: Total Appeal of A String
class Solution {
public long appealSum(String s) {
long ans = 0;
long t = 0;
int[] pos = new int[26];
Arrays.fill(pos, -1);
for (int i = 0; i < s.length(); ++i) {
int c = s.charAt(i) - 'a';
t += i - pos[c];
ans += t;
pos[c] = i;
}
return ans;
}
}
// Accepted solution for LeetCode #2262: Total Appeal of A String
func appealSum(s string) int64 {
var ans, t int64
pos := make([]int, 26)
for i := range pos {
pos[i] = -1
}
for i, c := range s {
c -= 'a'
t += int64(i - pos[c])
ans += t
pos[c] = i
}
return ans
}
# Accepted solution for LeetCode #2262: Total Appeal of A String
class Solution:
def appealSum(self, s: str) -> int:
ans = t = 0
pos = [-1] * 26
for i, c in enumerate(s):
c = ord(c) - ord('a')
t += i - pos[c]
ans += t
pos[c] = i
return ans
// Accepted solution for LeetCode #2262: Total Appeal of A String
/**
* [2262] Total Appeal of A String
*
* The appeal of a string is the number of distinct characters found in the string.
*
* For example, the appeal of "abbca" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'.
*
* Given a string s, return the total appeal of all of its substrings.
* A substring is a contiguous sequence of characters within a string.
*
* Example 1:
*
* Input: s = "abbca"
* Output: 28
* Explanation: The following are the substrings of "abbca":
* - Substrings of length 1: "a", "b", "b", "c", "a" have an appeal of 1, 1, 1, 1, and 1 respectively. The sum is 5.
* - Substrings of length 2: "ab", "bb", "bc", "ca" have an appeal of 2, 1, 2, and 2 respectively. The sum is 7.
* - Substrings of length 3: "abb", "bbc", "bca" have an appeal of 2, 2, and 3 respectively. The sum is 7.
* - Substrings of length 4: "abbc", "bbca" have an appeal of 3 and 3 respectively. The sum is 6.
* - Substrings of length 5: "abbca" has an appeal of 3. The sum is 3.
* The total sum is 5 + 7 + 7 + 6 + 3 = 28.
*
* Example 2:
*
* Input: s = "code"
* Output: 20
* Explanation: The following are the substrings of "code":
* - Substrings of length 1: "c", "o", "d", "e" have an appeal of 1, 1, 1, and 1 respectively. The sum is 4.
* - Substrings of length 2: "co", "od", "de" have an appeal of 2, 2, and 2 respectively. The sum is 6.
* - Substrings of length 3: "cod", "ode" have an appeal of 3 and 3 respectively. The sum is 6.
* - Substrings of length 4: "code" has an appeal of 4. The sum is 4.
* The total sum is 4 + 6 + 6 + 4 = 20.
*
*
* Constraints:
*
* 1 <= s.length <= 10^5
* s consists of lowercase English letters.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/total-appeal-of-a-string/
// discuss: https://leetcode.com/problems/total-appeal-of-a-string/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn appeal_sum(s: String) -> i64 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2262_example_1() {
let s = "abbca".to_string();
let result = 28;
assert_eq!(Solution::appeal_sum(s), result);
}
#[test]
#[ignore]
fn test_2262_example_2() {
let s = "code".to_string();
let result = 20;
assert_eq!(Solution::appeal_sum(s), result);
}
}
// Accepted solution for LeetCode #2262: Total Appeal of A String
function appealSum(s: string): number {
const pos: number[] = Array(26).fill(-1);
const n = s.length;
let ans = 0;
let t = 0;
for (let i = 0; i < n; ++i) {
const c = s.charCodeAt(i) - 97;
t += i - pos[c];
ans += t;
pos[c] = i;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.