File tree 2 files changed +41
-0
lines changed
2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 716
716
903|[ Valid Permutations for DI Sequence] ( ./0903-valid-permutations-for-di-sequence.js ) |Hard|
717
717
904|[ Fruit Into Baskets] ( ./0904-fruit-into-baskets.js ) |Medium|
718
718
905|[ Sort Array By Parity] ( ./0905-sort-array-by-parity.js ) |Easy|
719
+ 906|[ Super Palindromes] ( ./0906-super-palindromes.js ) |Hard|
719
720
909|[ Snakes and Ladders] ( ./0909-snakes-and-ladders.js ) |Medium|
720
721
912|[ Sort an Array] ( ./0912-sort-an-array.js ) |Medium|
721
722
914|[ X of a Kind in a Deck of Cards] ( ./0914-x-of-a-kind-in-a-deck-of-cards.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 906. Super Palindromes
3
+ * https://leetcode.com/problems/super-palindromes/
4
+ * Difficulty: Hard
5
+ *
6
+ * Let's say a positive integer is a super-palindrome if it is a palindrome, and it is also
7
+ * the square of a palindrome.
8
+ *
9
+ * Given two positive integers left and right represented as strings, return the number of
10
+ * super-palindromes integers in the inclusive range [left, right].
11
+ */
12
+
13
+ /**
14
+ * @param {string } left
15
+ * @param {string } right
16
+ * @return {number }
17
+ */
18
+ var superpalindromesInRange = function ( lowerBound , upperBound ) {
19
+ let result = 9n >= lowerBound && 9n <= upperBound ? 1 : 0 ;
20
+
21
+ const isPalindrome = sequence => {
22
+ for ( let start = 0 , end = sequence . length - 1 ; start < end ; start ++ , end -- ) {
23
+ if ( sequence [ start ] !== sequence [ end ] ) return false ;
24
+ }
25
+ return true ;
26
+ } ;
27
+
28
+ for ( let base = 1 ; base < 19684 ; base ++ ) {
29
+ const ternary = base . toString ( 3 ) ;
30
+ if ( isPalindrome ( ternary ) ) {
31
+ const square = BigInt ( ternary ) * BigInt ( ternary ) ;
32
+ if ( square > upperBound ) return result ;
33
+ if ( square >= lowerBound && isPalindrome ( square . toString ( ) ) ) {
34
+ result ++ ;
35
+ }
36
+ }
37
+ }
38
+
39
+ return result ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments