Skip to content

Commit 7b1300a

Browse files
committed
Add solution #906
1 parent c161f3d commit 7b1300a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@
716716
903|[Valid Permutations for DI Sequence](./0903-valid-permutations-for-di-sequence.js)|Hard|
717717
904|[Fruit Into Baskets](./0904-fruit-into-baskets.js)|Medium|
718718
905|[Sort Array By Parity](./0905-sort-array-by-parity.js)|Easy|
719+
906|[Super Palindromes](./0906-super-palindromes.js)|Hard|
719720
909|[Snakes and Ladders](./0909-snakes-and-ladders.js)|Medium|
720721
912|[Sort an Array](./0912-sort-an-array.js)|Medium|
721722
914|[X of a Kind in a Deck of Cards](./0914-x-of-a-kind-in-a-deck-of-cards.js)|Medium|

Diff for: solutions/0906-super-palindromes.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
};

0 commit comments

Comments
 (0)