Skip to content

Commit 074eb15

Browse files
committed
Runtime: 642 ms (Top 13.93%) | Memory: 67.4 MB (Top 22.14%)
1 parent 76b5884 commit 074eb15

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1+
// Runtime: 642 ms (Top 13.93%) | Memory: 67.4 MB (Top 22.14%)
12
var longestPalindrome = function(words) {
23
const n = words.length;
34
const map = new Map();
4-
5+
56
let len = 0;
6-
7+
78
for (const word of words) {
89
const backward = word.split("").reverse().join("");
9-
10+
1011
if (map.has(backward)) {
1112
len += (word.length * 2);
1213
map.set(backward, map.get(backward) - 1);
13-
14+
1415
if (map.get(backward) === 0) map.delete(backward);
1516
}
1617
else {
1718
if (!map.has(word)) map.set(word, 0);
1819
map.set(word, map.get(word) + 1);
1920
}
2021
}
21-
22+
2223
let maxLenSelfPalindrome = 0;
23-
24+
2425
for (const word of map.keys()) {
2526
if (isPalindrome(word)) {
2627
maxLenSelfPalindrome = Math.max(maxLenSelfPalindrome, word.length);
2728
}
28-
}
29-
29+
}
30+
3031
return len + maxLenSelfPalindrome;
31-
32+
3233
function isPalindrome(word) {
3334
let left = 0;
3435
let right = word.length - 1;
35-
36+
3637
while (left < right) {
3738
if (word[left] != word[right]) return false;
3839
left++;
3940
--right;
4041
}
41-
42+
4243
return true;
4344
}
44-
};
45+
};

0 commit comments

Comments
 (0)