Interview Questions/Coding/Broadcast Appeal Score

Broadcast Appeal Score

Preview mode. Log in to edit, run, submit, and save progress.

Hard

Broadcast Appeal Score

A broadcast message is analyzed by looking at every contiguous substring. The appeal of a substring is the number of distinct characters present inside that substring. For example, repeated copies of the same character inside one substring contribute only once to that substring's appeal. The final broadcast appeal score is the sum of appeal values over all possible non-empty substrings of the message. Input Format: You are given a lowercase string message. Output Format: Return the total appeal score across all substrings of message.

Examples

Example 1
Input:
message = "abbca"
Output:
28

Explanation: Total appeal of substrings ending at each index is [1, 3, 4, 8, 12]. Their sum is 28, so the output is 28.

Approach hint

Track the last seen index of each character.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp