Skip to content

Commit 3f4031c

Browse files
committed
Add solution #409
1 parent 8822329 commit 3f4031c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
405|[Convert a Number to Hexadecimal](./0405-convert-a-number-to-hexadecimal.js)|Easy|
327327
406|[Queue Reconstruction by Height](./0406-queue-reconstruction-by-height.js)|Medium|
328328
407|[Trapping Rain Water II](./0407-trapping-rain-water-ii.js)|Hard|
329+
409|[Longest Palindrome](./0409-longest-palindrome.js)|Easy|
329330
412|[Fizz Buzz](./0412-fizz-buzz.js)|Easy|
330331
414|[Third Maximum Number](./0414-third-maximum-number.js)|Easy|
331332
415|[Add Strings](./0415-add-strings.js)|Easy|

solutions/0409-longest-palindrome.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 409. Longest Palindrome
3+
* https://leetcode.com/problems/longest-palindrome/
4+
* Difficulty: Easy
5+
*
6+
* Given a string s which consists of lowercase or uppercase letters, return the length
7+
* of the longest palindrome that can be built with those letters.
8+
*
9+
* Letters are case sensitive, for example, "Aa" is not considered a palindrome.
10+
*/
11+
12+
/**
13+
* @param {string} s
14+
* @return {number}
15+
*/
16+
var longestPalindrome = function(s) {
17+
const set = new Set();
18+
return [...s].reduce((count, c) => {
19+
return set.delete(c) ? count + 2 : (set.add(c), count);
20+
}, 0) + (set.size > 0 ? 1 : 0);
21+
};

0 commit comments

Comments
 (0)