Design Gurus Logo
Count Unique Characters of All Substrings of a Given String (hard)

Problem Statement

Given a string s, return the sum of unique characters in all possible substrings of a given string. You also need to count the unique characters of repeating substrings.

Examples

  • Example 1:

    • Input: "aac"
    • Expected Output: 8
    • Justification: The substrings are "a", "a", "c", "aa", "ac", "aac". Their unique characters count are 1, 1, 1, 0, 2, 1 respectively. Summing up the unique characters: 1 + 1 + 1 + 0 (from "aa") + 2 (from "ac") + 1 (from "aac") = 6.
  • Example 2:

    • Input: "aaa"
    • Expected Output: 6
    • Justification: The substrings are "a", "a", "a", "aa", "aa", and "aaa". Their unique characters count are 1, 1, 1, 0, 0, 0 respectively. So, there are total 3 unique characters in all substrings.
  • Example 3:

    • Input: "abca"
    • Expected Output: 18
    • Justification: The substrings and their unique character counts are as follows: "a" (1), "b" (1), "c" (1), "a" (1), "ab" (2), "bc" (2), "ca" (2), "abc" (3), "bca" (3), "abca" (2). The sum of unique characters is 18.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory