Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Unique Characters of All Substrings of a Given String

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "828" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

Let's define a function countUniqueChars(s) that returns the number of unique characters in s. For example, calling countUniqueChars(s) if s = "LEETCODE" then "L", "T", "C", "O", "D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5. Given a string s, return the sum of countUniqueChars(t) where t is a substring of s. The test cases are generated such that the answer fits in a 32-bit integer. Notice that some substrings can be repeated so in this case you have to count the repeated ones too. Example 1: Input: s = "ABC" Output: 10 Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC". Every substring is composed with only unique letters. Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10 Example 2: Input: s = "ABA" Output: 8 Explanation: The same as example 1, except countUniqueChars("ABA") = 1. Example 3: Input: s = "LEETCODE" Output: 92 Constraints: 1 <= s.length <= 105 s consists of uppercase English letters only.

Explanation

Here's a breakdown of the approach and the Python code:

  • Key Idea: Instead of generating all substrings and then counting unique characters in each, we focus on each character in the string and calculate how many substrings it contributes to as a unique character.
  • Contribution Calculation: For each character, we find its left and right boundaries. The left boundary is the index of the closest identical character to its left (or -1 if none exists). The right boundary is the index of the closest identical character to its right (or the string length if none exists). The number of substrings where the character is unique is then (current index - left boundary) * (right boundary - current index).
  • Summation: We iterate through the string, calculating the contribution of each character and summing them up.

  • Complexity: Runtime: O(n), Storage: O(1)

Code

    def uniqueLetterString(s: str) -> int:
    n = len(s)
    ans = 0
    for i in range(n):
        left = -1
        for j in range(i - 1, -1, -1):
            if s[j] == s[i]:
                left = j
                break

        right = n
        for j in range(i + 1, n):
            if s[j] == s[i]:
                right = j
                break

        ans += (i - left) * (right - i)

    return ans

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Count Unique Characters of All Substrings of a Given String