Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Different Palindromic Subsequences

Updated
3 min read

Introduction

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

Given a string s, return the number of different non-empty palindromic subsequences in s. Since the answer may be very large, return it modulo 109 + 7. A subsequence of a string is obtained by deleting zero or more characters from the string. A sequence is palindromic if it is equal to the sequence reversed. Two sequences a1, a2, ... and b1, b2, ... are different if there is some i for which ai != bi. Example 1: Input: s = "bccb" Output: 6 Explanation: The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'. Note that 'bcb' is counted only once, even though it occurs twice. Example 2: Input: s = "abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba" Output: 104860361 Explanation: There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 109 + 7. Constraints: 1 <= s.length <= 1000 s[i] is either 'a', 'b', 'c', or 'd'.

Explanation

Here's a breakdown of the solution:

  • Dynamic Programming: Use a 2D DP table dp[i][j] to store the number of distinct palindromic subsequences within the substring s[i:j+1].
  • Base Cases and Transitions: If s[i] == s[j], then dp[i][j] = dp[i+1][j-1] * 2. We also need to handle overlapping palindromes based on the number of occurrences of s[i] (which is same as s[j]) within s[i+1:j]. If s[i] != s[j], then dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1].
  • Modulo Arithmetic: Apply the modulo operator (% (10**9 + 7)) throughout the calculations to prevent integer overflow.

  • Time and Space Complexity: O(n^2) time, O(n^2) space, where n is the length of the string s.

Code

    def countPalindromicSubsequences(s: str) -> int:
    n = len(s)
    dp = [[0] * n for _ in range(n)]
    mod = 10**9 + 7

    for i in range(n):
        dp[i][i] = 1

    for length in range(2, n + 1):
        for i in range(n - length + 1):
            j = i + length - 1
            if s[i] == s[j]:
                left = i + 1
                right = j - 1
                while left <= right and s[left] == s[i]:
                    left += 1
                while left <= right and s[right] == s[i]:
                    right -= 1

                if left > right:
                    dp[i][j] = (dp[i + 1][j - 1] * 2 + 2) % mod
                elif left == right:
                    dp[i][j] = (dp[i + 1][j - 1] * 2 + 1) % mod
                else:
                    dp[i][j] = (dp[i + 1][j - 1] * 2 - dp[left][right]) % mod
            else:
                dp[i][j] = (dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1]) % mod
            dp[i][j] = (dp[i][j] + mod) % mod

    return dp[0][n - 1]

More from this blog

C

Chatmagic blog

2894 posts