Advertisement

Find Palindrome With Fixed Length - LeetCode 2217 Solution

Find Palindrome With Fixed Length - Complete Solution Guide

Find Palindrome With Fixed Length is LeetCode problem 2217, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer array queries and a positive integer intLength , return an array answer where answer[i] is either the queries[i] th smallest positive palindrome of length intLength or -1 if no such palindrome exists . A palindrome is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros. Example 1: Input: queries = [1,2,3,4,5,90], intLength = 3 Output: [101,111,121,131,141,999] Explanation: The first few palindromes of length 3 are: 101, 111, 121, 131, 141,

Detailed Explanation

The problem asks us to find the `queries[i]`-th smallest palindrome of a given length `intLength`. If the palindrome exists, we return it; otherwise, we return -1. A palindrome is a number that reads the same forward and backward, and cannot have leading zeros. The input is an array of integers `queries` and an integer `intLength`. The output is an array of integers where each element is the `queries[i]`-th smallest palindrome of length `intLength`, or -1 if it doesn't exist.

Solution Approach

The solution constructs the `queries[i]`-th palindrome by first determining the base number and then calculating the first half of the palindrome. If the `queries[i]`-th palindrome exists, its first half is `base + queries[i] - 1`. We convert the first half to a string, and mirror the necessary part of it to create the second half of the palindrome. These two halves are then concatenated and converted to an integer to form the final palindrome.

Step-by-Step Algorithm

  1. Step 1: Calculate the length of the first half of the palindrome: `half_length = (intLength + 1) // 2`.
  2. Step 2: Calculate the base number for the first half of the palindrome. This is simply 10 raised to the power of `half_length - 1`. This value will be used as the starting point when calculating our palindrome.
  3. Step 3: Determine the total count of possible palindromes of length `intLength`. The count is `9 * (10 ** (half_length - 1))`. This allows us to check if a given `query` will generate a valid palindrome without needing to calculate it.
  4. Step 4: Iterate through the `queries` array. For each `query`, check if it exceeds the `count`. If it does, append -1 to the `answer` array.
  5. Step 5: If the `query` is valid (i.e., it does not exceed `count`), calculate the first half of the palindrome using `first_half_num = base + query - 1`.
  6. Step 6: Convert the `first_half_num` to a string `first_half_str`.
  7. Step 7: Determine the second half of the palindrome by reversing the necessary portion of `first_half_str`. The length of this substring is `intLength // 2`.
  8. Step 8: Concatenate `first_half_str` and `second_half_str` to form the complete palindrome string `palindrome_str`.
  9. Step 9: Convert the `palindrome_str` to an integer and append it to the `answer` array.
  10. Step 10: Return the `answer` array.

Key Insights

  • Insight 1: Palindromes can be constructed from their first half. For even length palindromes, the first half is mirrored to create the second half. For odd length palindromes, the first (n+1)/2 digits determine the entire palindrome.
  • Insight 2: We can determine the smallest and largest possible palindrome for a given length `intLength`. This enables us to quickly check if the `queries[i]`-th palindrome exists without generating all possible palindromes.
  • Insight 3: Efficiently generate the i-th palindrome by constructing its first half directly, avoiding an exhaustive search through all numbers.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Math.

Companies

Asked at: VMware.