Skip to content

Commit ede6131

Browse files
authored
Merge pull request #3840 from katarianikita2003/main
Create 0906-Super-Palindromes.md
2 parents 48b7bc7 + a304b97 commit ede6131

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
---
2+
id: super-palindromes
3+
title: Super Palindromes
4+
sidebar_label: Super Palindromes
5+
tags:
6+
- Palindrome
7+
- Math
8+
---
9+
10+
## Problem Description
11+
12+
| Problem Statement | Solution Link | LeetCode Profile |
13+
| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ |
14+
| [Super Palindromes](https://leetcode.com/problems/super-palindromes/description/) | [Super Palindromes Solution on LeetCode](https://leetcode.com/problems/super-palindromes/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) |
15+
16+
## Problem Description
17+
18+
Let's say a positive integer is a super-palindrome if it is a palindrome, and it is also the square of a palindrome.
19+
20+
Given two positive integers `left` and `right` represented as strings, return the number of super-palindromes integers in the inclusive range `[left, right]`.
21+
22+
### Examples
23+
24+
**Example 1:**
25+
26+
**Input:** `left = "4" right = "1000"`
27+
**Output:** `4`
28+
**Explanation:** `4, 9, 121, and 484 are super-palindromes. Note that 676 is not a super-palindrome: 26 * 26 = 676, but 26 is not a palindrome.`
29+
30+
**Example 2:**
31+
32+
**Input:** `left = "1" right = "2"`
33+
**Output:** `1`
34+
35+
### Constraints
36+
37+
- `1 <= left.length, right.length <= 18`
38+
- `left` and `right` consist of only digits.
39+
- `left` and `right` cannot have leading zeros.
40+
- `left` and `right` represent integers in the range `[1, 10^18 - 1]`.
41+
- `left` is less than or equal to `right`.
42+
43+
## Approach
44+
45+
To solve this problem, we need to:
46+
47+
1. **Generate Palindromes:** Generate all palindromes that are within the possible square roots of the range `[left, right]`.
48+
2. **Square Palindromes:** For each generated palindrome, compute its square and check if it is a palindrome.
49+
3. **Count Super-Palindromes:** Count how many of these squared palindromes fall within the given range `[left, right]`.
50+
51+
## Solution
52+
53+
### Python
54+
55+
```python
56+
import math
57+
58+
def is_palindrome(s):
59+
return s == s[::-1]
60+
61+
def super_palindromes(left, right):
62+
left, right = int(left), int(right)
63+
count = 0
64+
limit = int(math.sqrt(right)) + 1
65+
66+
for i in range(1, limit):
67+
s = str(i)
68+
pal = s + s[::-1]
69+
num = int(pal) ** 2
70+
if num > right:
71+
break
72+
if num >= left and is_palindrome(str(num)):
73+
count += 1
74+
75+
pal = s + s[-2::-1]
76+
num = int(pal) ** 2
77+
if num > right:
78+
break
79+
if num >= left and is_palindrome(str(num)):
80+
count += 1
81+
82+
return count
83+
```
84+
85+
### Java
86+
87+
```java
88+
public class SuperPalindromes {
89+
private static boolean isPalindrome(String s) {
90+
int left = 0, right = s.length() - 1;
91+
while (left < right) {
92+
if (s.charAt(left) != s.charAt(right)) return false;
93+
left++;
94+
right--;
95+
}
96+
return true;
97+
}
98+
99+
public static int superPalindromes(String left, String right) {
100+
long l = Long.parseLong(left), r = Long.parseLong(right);
101+
int count = 0;
102+
long limit = (long) Math.sqrt(r) + 1;
103+
104+
for (long i = 1; i < limit; i++) {
105+
String s = Long.toString(i);
106+
String pal1 = s + new StringBuilder(s).reverse().toString();
107+
long num1 = Long.parseLong(pal1) * Long.parseLong(pal1);
108+
if (num1 > r) break;
109+
if (num1 >= l && isPalindrome(Long.toString(num1))) count++;
110+
111+
String pal2 = s + new StringBuilder(s.substring(0, s.length() - 1)).reverse().toString();
112+
long num2 = Long.parseLong(pal2) * Long.parseLong(pal2);
113+
if (num2 > r) break;
114+
if (num2 >= l && isPalindrome(Long.toString(num2))) count++;
115+
}
116+
117+
return count;
118+
}
119+
}
120+
```
121+
122+
### C++
123+
124+
```cpp
125+
#include <cmath>
126+
#include <string>
127+
#include <algorithm>
128+
129+
bool isPalindrome(const std::string& s) {
130+
return std::equal(s.begin(), s.begin() + s.size() / 2, s.rbegin());
131+
}
132+
133+
int superPalindromes(std::string left, std::string right) {
134+
long long l = std::stoll(left), r = std::stoll(right);
135+
int count = 0;
136+
long long limit = static_cast<long long>(std::sqrt(r)) + 1;
137+
138+
for (long long i = 1; i < limit; i++) {
139+
std::string s = std::to_string(i);
140+
std::string pal1 = s + std::string(s.rbegin(), s.rend());
141+
long long num1 = std::stoll(pal1) * std::stoll(pal1);
142+
if (num1 > r) break;
143+
if (num1 >= l && isPalindrome(std::to_string(num1))) count++;
144+
145+
std::string pal2 = s + std::string(s.rbegin() + 1, s.rend());
146+
long long num2 = std::stoll(pal2) * std::stoll(pal2);
147+
if (num2 > r) break;
148+
if (num2 >= l && isPalindrome(std::to_string(num2))) count++;
149+
}
150+
151+
return count;
152+
}
153+
```
154+
155+
### C
156+
157+
```c
158+
#include <stdio.h>
159+
#include <stdlib.h>
160+
#include <stdbool.h>
161+
#include <string.h>
162+
#include <math.h>
163+
164+
bool isPalindrome(char* s) {
165+
int len = strlen(s);
166+
for (int i = 0; i < len / 2; i++) {
167+
if (s[i] != s[len - i - 1]) return false;
168+
}
169+
return true;
170+
}
171+
172+
int superPalindromes(char* left, char* right) {
173+
long long l = atoll(left), r = atoll(right);
174+
int count = 0;
175+
long long limit = (long long) sqrt(r) + 1;
176+
177+
char s[20], pal[40];
178+
for (long long i = 1; i < limit; i++) {
179+
sprintf(s, "%lld", i);
180+
int len = strlen(s);
181+
182+
// Palindrome of even length
183+
snprintf(pal, 40, "%s%s", s, strrev(strdup(s)));
184+
long long num1 = atoll(pal) * atoll(pal);
185+
if (num1 > r) break;
186+
if (num1 >= l && isPalindrome(ltoa(num1, pal, 10))) count++;
187+
188+
// Palindrome of odd length
189+
snprintf(pal, 40, "%s%s", s, strrev(strdup(s) + 1));
190+
long long num2 = atoll(pal) * atoll(pal);
191+
if (num2 > r) break;
192+
if (num2 >= l && isPalindrome(ltoa(num2, pal, 10))) count++;
193+
}
194+
195+
return count;
196+
}
197+
```
198+
199+
### JavaScript
200+
201+
```javascript
202+
function isPalindrome(s) {
203+
return s === s.split('').reverse().join('');
204+
}
205+
206+
function superPalindromes(left, right) {
207+
let l = BigInt(left), r = BigInt(right);
208+
let count = 0;
209+
let limit = BigInt(Math.sqrt(Number(r))) + BigInt(1);
210+
211+
for (let i = BigInt(1); i < limit; i++) {
212+
let s = i.toString();
213+
let pal1 = s + s.split('').reverse().join('');
214+
let num1 = BigInt(pal1) ** BigInt(2);
215+
if (num1 > r) break;
216+
if (num1 >= l && isPalindrome(num1.toString())) count++;
217+
218+
let pal2 = s + s.slice(0, -1).split('').reverse().join('');
219+
let num2 = BigInt(pal2) ** BigInt(2);
220+
if (num2 > r) break;
221+
if (num2 >= l && isPalindrome(num2.toString())) count++;
222+
}
223+
224+
return count;
225+
}
226+
```
227+
228+
## Step-by-Step Algorithm
229+
230+
1. **Generate Palindromes:**
231+
- Iterate through possible values of `i` from 1 to the square root of the right boundary.
232+
- Construct palindromes by concatenating `s` with its reverse and with its reverse minus the last character.
233+
234+
2. **Square Palindromes:**
235+
- Compute the square of each palindrome.
236+
- Check if the squared value is within the range `[left, right]`.
237+
238+
3. **Check for Super-Palindromes:**
239+
- Verify if the squared palindrome is also a palindrome.
240+
241+
4. **Count and Return:**
242+
- Count valid super-palindromes and return the count.
243+
244+
## Conclusion
245+
246+
The problem of finding super-palindromes can be efficiently solved by generating palindromes and checking their squares for the required properties. This approach ensures that we only work within the necessary range and use string manipulations to identify palindromic numbers, which is both effective and computationally feasible.

0 commit comments

Comments
 (0)