22id : super-palindromes
33title : Super Palindromes
44sidebar_label : Super Palindromes
5- tags :
6- - Palindrome
7- - Math
5+ tags :
6+ - Palindrome
7+ - Math
88---
99
1010## Problem Description
1111
12- | Problem Statement | Solution Link | LeetCode Profile |
13- | :-------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------- | :--------------------------------------------------- |
12+ | Problem Statement | Solution Link | LeetCode Profile |
13+ | :------------------------------------------------------ | :------------------------------------------------------------------------- | :--- --------------------------------------------------- |
1414| [ 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/ ) |
1515
1616## Problem Description
@@ -62,7 +62,7 @@ def super_palindromes(left, right):
6262 left, right = int (left), int (right)
6363 count = 0
6464 limit = int (math.sqrt(right)) + 1
65-
65+
6666 for i in range (1 , limit):
6767 s = str (i)
6868 pal = s + s[::- 1 ]
@@ -71,14 +71,14 @@ def super_palindromes(left, right):
7171 break
7272 if num >= left and is_palindrome(str (num)):
7373 count += 1
74-
74+
7575 pal = s + s[- 2 ::- 1 ]
7676 num = int (pal) ** 2
7777 if num > right:
7878 break
7979 if num >= left and is_palindrome(str (num)):
8080 count += 1
81-
81+
8282 return count
8383```
8484
@@ -178,7 +178,7 @@ int superPalindromes(char* left, char* right) {
178178 for (long long i = 1; i < limit; i++) {
179179 sprintf(s, "%lld", i);
180180 int len = strlen(s);
181-
181+
182182 // Palindrome of even length
183183 snprintf(pal, 40, "%s%s", s, strrev(strdup(s)));
184184 long long num1 = atoll(pal) * atoll(pal);
@@ -200,29 +200,28 @@ int superPalindromes(char* left, char* right) {
200200
201201``` javascript
202202function isPalindrome (s ) {
203- return s === s .split (" " ).reverse ().join (" " );
203+ return s === s .split (' ' ).reverse ().join (' ' );
204204}
205205
206206function 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;
226225}
227226```
228227
@@ -231,13 +230,12 @@ function superPalindromes(left, right) {
2312301 . ** Generate Palindromes:**
232231 - Iterate through possible values of ` i ` from 1 to the square root of the right boundary.
233232 - Construct palindromes by concatenating ` s ` with its reverse and with its reverse minus the last character.
233+
2342342 . ** Square Palindromes:**
235-
236235 - Compute the square of each palindrome.
237236 - Check if the squared value is within the range ` [left, right] ` .
238237
2392383 . ** Check for Super-Palindromes:**
240-
241239 - Verify if the squared palindrome is also a palindrome.
242240
2432414 . ** Count and Return:**
0 commit comments