Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Find the Original Typed String II

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3333" 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

Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and may press a key for too long, resulting in a character being typed multiple times. You are given a string word, which represents the final output displayed on Alice's screen. You are also given a positive integer k. Return the total number of possible original strings that Alice might have intended to type, if she was trying to type a string of size at least k. Since the answer may be very large, return it modulo 109 + 7. Example 1: Input: word = "aabbccdd", k = 7 Output: 5 Explanation: The possible strings are: "aabbccdd", "aabbccd", "aabbcdd", "aabccdd", and "abbccdd". Example 2: Input: word = "aabbccdd", k = 8 Output: 1 Explanation: The only possible string is "aabbccdd". Example 3: Input: word = "aaabbb", k = 3 Output: 8 Constraints: 1 <= word.length <= 5 * 105 word consists only of lowercase English letters. 1 <= k <= 2000

Explanation

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

  • Identify Repeating Groups: First, compress the input word into groups of consecutive identical characters. This gives us the minimum length of the original string.
  • Dynamic Programming: Use dynamic programming to calculate the number of possible original strings with length at least k. The DP state represents the index of the group and the current length of the string being formed.
  • Modulo Arithmetic: Apply modulo arithmetic (10**9 + 7) at each step to prevent integer overflow.

  • Time Complexity: O(n k), where n is the number of repeating groups in the input string word, and k is the input integer. Space Complexity: O(n k)

Code

    def count_possible_strings(word: str, k: int) -> int:
    """
    Counts the number of possible original strings Alice might have typed.

    Args:
        word: The final output displayed on Alice's screen.
        k: The minimum length of the original string.

    Returns:
        The total number of possible original strings modulo 10^9 + 7.
    """

    MOD = 10**9 + 7
    groups = []
    count = 1
    for i in range(1, len(word)):
        if word[i] == word[i - 1]:
            count += 1
        else:
            groups.append(count)
            count = 1
    groups.append(count)

    n = len(groups)
    min_len = n
    dp = [[0] * (k + 1) for _ in range(n + 1)]
    dp[0][0] = 1

    for i in range(1, n + 1):
        for j in range(k + 1):
            for l in range(1, min(j + 1, groups[i - 1] + 1)):
                dp[i][j] = (dp[i][j] + dp[i - 1][j - l]) % MOD

    ans = 0
    for j in range(k, k + 1):
        if j < len(dp[n]):
            ans = (ans + dp[n][j]) % MOD

    if k > min_len:
        for j in range(k, len(dp[n])):
            ans = (ans + dp[n][j]) % MOD
        return ans

    for j in range(k, k + 1):
        if j < len(dp[n]):
            ans = (ans + dp[n][j]) % MOD

    if k <= min_len:
      dp = [[0] * (len(word) + 1) for _ in range(n + 1)]
      dp[0][0] = 1
      for i in range(1, n + 1):
          for j in range(len(word) + 1):
              for l in range(1, min(j + 1, groups[i - 1] + 1)):
                  dp[i][j] = (dp[i][j] + dp[i - 1][j - l]) % MOD

      ans = 0
      for j in range(k, len(word) + 1):
          ans = (ans + dp[n][j]) % MOD
      return ans

    return ans

More from this blog

C

Chatmagic blog

2894 posts