Sum the unique-character counts over every substring of a string

Quick Overview

Given an uppercase string, add up the number of characters that appear exactly once in each of its substrings, counting repeated substrings at every position. It tests whether you can avoid enumerating all substrings and reach a linear-time solution for strings of up to 100,000 characters.

Sum the unique-character counts over every substring of a string

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

For a string `t`, let `count_unique_chars(t)` be the number of characters that appear exactly once in `t`. For example, in `"LEETCODE"` the characters `L`, `T`, `C`, `O` and `D` each appear once, so `count_unique_chars("LEETCODE") = 5`. Given a string `s`, return the sum of `count_unique_chars(t)` over every substring `t` of `s`. Some substrings have the same text at different positions, and each of those occurrences is counted. The test cases are generated so that the answer fits in a 32-bit signed integer. ### Function Signature ```python def sum_unique_char_counts(s: str) -> int: ... ``` ### Rules - A substring is a non-empty contiguous slice `s[i:j]` with `0 <= i < j <= len(s)`. A string of length `n` has `n * (n + 1) / 2` of them, and each is counted once per position, even when two of them have the same text. - A character counts toward `count_unique_chars(t)` only if it occurs exactly once in `t`. A letter that appears two or more times in `t` adds nothing for that substring. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists of uppercase English letters only. ### Examples ```text Input: s = "ABC" Output: 10 ``` The six substrings are "A", "B", "C", "AB", "BC" and "ABC". No letter repeats in any of them, so their counts are 1, 1, 1, 2, 2 and 3, which sum to 10. ```text Input: s = "ABA" Output: 8 ``` The substrings "A", "B", "A", "AB" and "BA" give 1, 1, 1, 2 and 2. In "ABA" only "B" appears once, so it gives 1, and the total is 8. ```text Input: s = "LEETCODE" Output: 92 ``` The string has 36 substrings. Their counts include 1 for "L", 0 for "EE" and 5 for the whole string, and all 36 add up to 92.

Overview: Given an uppercase string, add up the number of characters that appear exactly once in each of its substrings, counting repeated substrings at every position. It tests whether you can avoid enumerating all substrings and reach a linear-time solution for strings of up to 100,000 characters.

Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

For a string t, let count_unique_chars(t) be the number of characters that appear exactly once in t. For example, in "LEETCODE" the characters L, T, C, O and D each appear once, so count_unique_chars("LEETCODE") = 5.

Given a string s, return the sum of count_unique_chars(t) over every substring t of s. Some substrings have the same text at different positions, and each of those occurrences is counted. The test cases are generated so that the answer fits in a 32-bit signed integer.

Function Signature

def sum_unique_char_counts(s: str) -> int:
    ...

Rules

  • A substring is a non-empty contiguous slice s[i:j] with 0 <= i < j <= len(s) . A string of length n has n * (n + 1) / 2 of them, and each is counted once per position, even when two of them have the same text.
  • A character counts toward count_unique_chars(t) only if it occurs exactly once in t . A letter that appears two or more times in t adds nothing for that substring.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists of uppercase English letters only.

Examples

Input: s = "ABC"
Output: 10

The six substrings are "A", "B", "C", "AB", "BC" and "ABC". No letter repeats in any of them, so their counts are 1, 1, 1, 2, 2 and 3, which sum to 10.

Input: s = "ABA"
Output: 8

The substrings "A", "B", "A", "AB" and "BA" give 1, 1, 1, 2 and 2. In "ABA" only "B" appears once, so it gives 1, and the total is 8.

Input: s = "LEETCODE"
Output: 92

The string has 36 substrings. Their counts include 1 for "L", 0 for "EE" and 5 for the whole string, and all 36 add up to 92.