2217. Find Palindrome With Fixed Length LeetCode Solution
In this guide, you will get 2217. Find Palindrome With Fixed Length LeetCode Solution with the best time and space complexity. The solution to Find Palindrome With Fixed Length problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find Palindrome With Fixed Length solution in C++
- Find Palindrome With Fixed Length solution in Java
- Find Palindrome With Fixed Length solution in Python
- Additional Resources
Problem Statement of Find Palindrome With Fixed Length
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, 151, 161, 171, 181, 191, 202, …
The 90th palindrome of length 3 is 999.
Example 2:
Input: queries = [2,4,6], intLength = 4
Output: [1111,1331,1551]
Explanation:
The first six palindromes of length 4 are:
1001, 1111, 1221, 1331, 1441, and 1551.
Constraints:
1 <= queries.length <= 5 * 104
1 <= queries[i] <= 109
1 <= intLength <= 15
Complexity Analysis
- Time Complexity: O(q)
- Space Complexity: O(q)
2217. Find Palindrome With Fixed Length LeetCode Solution in C++
class Solution {
public:
vector<long long> kthPalindrome(vector<int>& queries, int intLength) {
const int start = pow(10, (intLength + 1) / 2 - 1);
const int end = pow(10, (intLength + 1) / 2);
const int mul = pow(10, intLength / 2);
vector<long long> ans;
for (const int query : queries)
if (start + query > end)
ans.push_back(-1);
else
ans.push_back(getKthPalindrome(query, start, mul, intLength));
return ans;
}
private:
long getKthPalindrome(int query, int start, int mul, int intLength) {
const long prefix = start + query - 1;
return prefix * mul + reverse(intLength % 2 == 0 ? prefix : prefix / 10);
}
long reverse(int num) {
long res = 0;
while (num > 0) {
res = res * 10 + num % 10;
num /= 10;
}
return res;
}
};
/* code provided by PROGIEZ */
2217. Find Palindrome With Fixed Length LeetCode Solution in Java
class Solution {
public long[] kthPalindrome(int[] queries, int intLength) {
final int start = (int) Math.pow(10, (intLength + 1) / 2 - 1);
final int end = (int) Math.pow(10, (intLength + 1) / 2);
final int mul = (int) Math.pow(10, intLength / 2);
long[] ans = new long[queries.length];
for (int i = 0; i < queries.length; ++i)
if (start + queries[i] > end)
ans[i] = -1;
else
ans[i] = getKthPalindrome(queries[i], start, mul, intLength);
return ans;
}
private long getKthPalindrome(int q, int start, int mul, int intLength) {
final long prefix = start + q - 1;
return prefix * mul + reverse(intLength % 2 == 0 ? prefix : prefix / 10);
}
long reverse(long num) {
long res = 0;
while (num > 0) {
res = res * 10 + num % 10;
num /= 10;
}
return res;
}
}
// code provided by PROGIEZ
2217. Find Palindrome With Fixed Length LeetCode Solution in Python
class Solution:
def kthPalindrome(self, queries: list[int], intLength: int) -> list[int]:
start = pow(10, (intLength + 1) // 2 - 1)
end = pow(10, (intLength + 1) // 2)
mul = pow(10, intLength // 2)
def reverse(num: int) -> int:
res = 0
while num:
res = res * 10 + num % 10
num //= 10
return res
def getKthPalindrome(query: int) -> int:
prefix = start + query - 1
return prefix * mul + reverse(prefix
if intLength % 2 == 0 else prefix // 10)
return [-1 if start + query > end else getKthPalindrome(query)
for query in queries]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.