Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Can Make Palindrome from Substring

Updated
3 min read

Introduction

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

You are given a string s and array queries where queries[i] = [lefti, righti, ki]. We may rearrange the substring s[lefti...righti] for each query and then choose up to ki of them to replace with any lowercase English letter. If the substring is possible to be a palindrome string after the operations above, the result of the query is true. Otherwise, the result is false. Return a boolean array answer where answer[i] is the result of the ith query queries[i]. Note that each letter is counted individually for replacement, so if, for example s[lefti...righti] = "aaa", and ki = 2, we can only replace two of the letters. Also, note that no query modifies the initial string s. Example : Input: s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]] Output: [true,false,false,true,true] Explanation: queries[0]: substring = "d", is palidrome. queries[1]: substring = "bc", is not palidrome. queries[2]: substring = "abcd", is not palidrome after replacing only 1 character. queries[3]: substring = "abcd", could be changed to "abba" which is palidrome. Also this can be changed to "baab" first rearrange it "bacd" then replace "cd" with "ab". queries[4]: substring = "abcda", could be changed to "abcba" which is palidrome. Example 2: Input: s = "lyb", queries = [[0,1,0],[2,2,1]] Output: [false,true] Constraints: 1 <= s.length, queries.length <= 105 0 <= lefti <= righti < s.length 0 <= ki <= s.length s consists of lowercase English letters.

Explanation

Here's the solution:

  • Prefix XOR Counts: Maintain a prefix XOR array for each character in the alphabet. prefix[i][j] stores the XOR of counts of the j-th character ('a' + j) up to index i in the string s. This allows for efficient calculation of character counts within a substring using XOR properties.

  • Palindrome Check with Replacement: For each query, determine the character counts within the specified substring using the prefix XOR array. Count the number of characters that appear an odd number of times. If this count is less than or equal to 2 * k + 1, then it's possible to form a palindrome (or a string that can be turned into a palindrome within the allowed replacements). We divide the count by 2 as we can replace two different odd counts with two copies of a new character to remove two odd counts.

  • Early Exit: If 2*k is greater or equal to the substring length, we can always make the substring a palindrome, hence return True immediately.

  • Runtime and Storage Complexity:

    • Runtime Complexity: O(N + M), where N is the length of the string s and M is the number of queries.
    • Storage Complexity: O(N), due to the prefix XOR array.

Code

    def canMakePaliQueries(s: str, queries: list[list[int]]) -> list[bool]:
    n = len(s)
    prefix = [[0] * 26 for _ in range(n + 1)]

    for i in range(n):
        for j in range(26):
            prefix[i + 1][j] = prefix[i][j]
        prefix[i + 1][ord(s[i]) - ord('a')] ^= 1

    result = []
    for left, right, k in queries:
        length = right - left + 1
        if 2 * k >= length:
            result.append(True)
            continue

        odd_count = 0
        for j in range(26):
            odd_count += prefix[right + 1][j] ^ prefix[left][j]

        odd_count = bin(odd_count).count('1')

        result.append(odd_count // 2 <= k)

    return result

More from this blog

C

Chatmagic blog

2894 posts