Skip to content

Commit 67f7b27

Browse files
authored
Update 906-super-palindromes.js
1 parent cddd0cb commit 67f7b27

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

906-super-palindromes.js

+43
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,46 @@ const isPalindromeInt = function (nr) {
110110
const isPalindrome = function (nr) {
111111
return nr === nr.split('').reverse().join('')
112112
}
113+
114+
// another
115+
116+
/**
117+
* @param {string} left
118+
* @param {string} right
119+
* @return {number}
120+
*/
121+
const superpalindromesInRange = function(left, right) {
122+
const palindromes = []
123+
let res = 0
124+
for(let i = 1; i < 10; i++) {
125+
palindromes.push(`${i}`)
126+
}
127+
for(let i = 1; i < 1e4; i++) {
128+
let l = `${i}`, r = l.split('').reverse().join('')
129+
palindromes.push(`${l}${r}`)
130+
for(let j = 0; j < 10; j++) {
131+
palindromes.push(`${l}${j}${r}`)
132+
}
133+
}
134+
135+
for(let p of palindromes) {
136+
const square = BigInt(p) * BigInt(p)
137+
if(!isPalindrome(`${square}`)) continue
138+
if(BigInt(left) <= square && square <= BigInt(right)) res++
139+
}
140+
141+
return res
142+
143+
function isPalindrome(str) {
144+
let i = 0;
145+
let j = str.length - 1;
146+
while (i < j) {
147+
if (str.charAt(i) !== str.charAt(j)) {
148+
return false;
149+
}
150+
i++;
151+
j--;
152+
}
153+
return true;
154+
}
155+
};

0 commit comments

Comments
 (0)