2
2
id : super-palindromes
3
3
title : Super Palindromes
4
4
sidebar_label : Super Palindromes
5
- tags :
6
- - Palindrome
7
- - Math
5
+ tags :
6
+ - Palindrome
7
+ - Math
8
8
---
9
9
10
10
## Problem Description
11
11
12
- | Problem Statement | Solution Link | LeetCode Profile |
13
- | :-------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------- | :--------------------------------------------------- |
12
+ | Problem Statement | Solution Link | LeetCode Profile |
13
+ | :------------------------------------------------------ | :------------------------------------------------------------------------- | :--- --------------------------------------------------- |
14
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
15
16
16
## Problem Description
@@ -62,7 +62,7 @@ def super_palindromes(left, right):
62
62
left, right = int (left), int (right)
63
63
count = 0
64
64
limit = int (math.sqrt(right)) + 1
65
-
65
+
66
66
for i in range (1 , limit):
67
67
s = str (i)
68
68
pal = s + s[::- 1 ]
@@ -71,14 +71,14 @@ def super_palindromes(left, right):
71
71
break
72
72
if num >= left and is_palindrome(str (num)):
73
73
count += 1
74
-
74
+
75
75
pal = s + s[- 2 ::- 1 ]
76
76
num = int (pal) ** 2
77
77
if num > right:
78
78
break
79
79
if num >= left and is_palindrome(str (num)):
80
80
count += 1
81
-
81
+
82
82
return count
83
83
```
84
84
@@ -178,7 +178,7 @@ int superPalindromes(char* left, char* right) {
178
178
for (long long i = 1; i < limit; i++) {
179
179
sprintf(s, "%lld", i);
180
180
int len = strlen(s);
181
-
181
+
182
182
// Palindrome of even length
183
183
snprintf(pal, 40, "%s%s", s, strrev(strdup(s)));
184
184
long long num1 = atoll(pal) * atoll(pal);
@@ -200,29 +200,28 @@ int superPalindromes(char* left, char* right) {
200
200
201
201
``` javascript
202
202
function isPalindrome (s ) {
203
- return s === s .split (" " ).reverse ().join (" " );
203
+ return s === s .split (' ' ).reverse ().join (' ' );
204
204
}
205
205
206
206
function superPalindromes (left , right ) {
207
- let l = BigInt (left),
208
- r = BigInt (right);
209
- let count = 0 ;
210
- let limit = BigInt (Math .sqrt (Number (r))) + BigInt (1 );
211
-
212
- for (let i = BigInt (1 ); i < limit; i++ ) {
213
- let s = i .toString ();
214
- let pal1 = s + s .split (" " ).reverse ().join (" " );
215
- let num1 = BigInt (pal1) ** BigInt (2 );
216
- if (num1 > r) break ;
217
- if (num1 >= l && isPalindrome (num1 .toString ())) count++ ;
218
-
219
- let pal2 = s + s .slice (0 , - 1 ).split (" " ).reverse ().join (" " );
220
- let num2 = BigInt (pal2) ** BigInt (2 );
221
- if (num2 > r) break ;
222
- if (num2 >= l && isPalindrome (num2 .toString ())) count++ ;
223
- }
224
-
225
- return count;
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;
226
225
}
227
226
```
228
227
@@ -231,13 +230,12 @@ function superPalindromes(left, right) {
231
230
1 . ** Generate Palindromes:**
232
231
- Iterate through possible values of ` i ` from 1 to the square root of the right boundary.
233
232
- Construct palindromes by concatenating ` s ` with its reverse and with its reverse minus the last character.
233
+
234
234
2 . ** Square Palindromes:**
235
-
236
235
- Compute the square of each palindrome.
237
236
- Check if the squared value is within the range ` [left, right] ` .
238
237
239
238
3 . ** Check for Super-Palindromes:**
240
-
241
239
- Verify if the squared palindrome is also a palindrome.
242
240
243
241
4 . ** Count and Return:**
0 commit comments