Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Construct K Palindrome Strings

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1400" 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 and an integer k, return true if you can use all the characters in s to construct non-empty k palindrome strings or false otherwise. Example 1: Input: s = "annabelle", k = 2 Output: true Explanation: You can construct two palindromes using all characters in s. Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b" Example 2: Input: s = "leetcode", k = 3 Output: false Explanation: It is impossible to construct 3 palindromes using all the characters of s. Example 3: Input: s = "true", k = 4 Output: true Explanation: The only possible solution is to put each character in a separate string. Constraints: 1 <= s.length <= 105 s consists of lowercase English letters. 1 <= k <= 105

Explanation

Here's the solution:

  • High-Level Approach:

    • Count the frequency of each character in the string.
    • Determine the number of characters that appear an odd number of times. A palindrome can only have at most one character appearing an odd number of times. If we need to create k palindromes, we need at most k characters appearing an odd number of times.
    • If the number of odd-count characters is greater than k, it's impossible to form k palindromes. Also, if the string length is less than k, it's not possible, as each character would need its own palindrome.
  • Complexity:

    • Time Complexity: O(n), where n is the length of the string.
    • Space Complexity: O(1), as the character frequency map has a constant size (26 for lowercase English letters).

Code

    def canConstruct(s: str, k: int) -> bool:
    """
    Given a string s and an integer k, return true if you can use all the characters in s to construct non-empty k palindrome strings or false otherwise.

    Example 1:
    Input: s = "annabelle", k = 2
    Output: true
    Explanation: You can construct two palindromes using all characters in s.
    Some possible constructions "anna" + "elble", "anbna" + "elle", "anellena" + "b"

    Example 2:
    Input: s = "leetcode", k = 3
    Output: false
    Explanation: It is impossible to construct 3 palindromes using all the characters of s.

    Example 3:
    Input: s = "true", k = 4
    Output: true
    Explanation: The only possible solution is to put each character in a separate string.

    Constraints:
    1 <= s.length <= 105
    s consists of lowercase English letters.
    1 <= k <= 105
    """
    if k > len(s):
        return False

    char_counts = {}
    for char in s:
        char_counts[char] = char_counts.get(char, 0) + 1

    odd_count = 0
    for count in char_counts.values():
        if count % 2 != 0:
            odd_count += 1

    return odd_count <= k

More from this blog

C

Chatmagic blog

2894 posts