Skip to content

Commit 006735e

Browse files
easy: 409. Longest Palindrome
1 parent 78beafc commit 006735e

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var longestPalindrome = function (s) {
6+
const charCounts = new Map();
7+
let numOddChars = 0;
8+
9+
for (let char of s) {
10+
const count = charCounts.get(char) || 0;
11+
charCounts.set(char, count + 1);
12+
13+
// Adjust numOddChars based on the new count
14+
numOddChars = ((count + 1) % 2 === 1) ? numOddChars + 1 : numOddChars - 1;
15+
}
16+
17+
// If there are odd-count characters, we can use all characters minus (numOddChars - 1)
18+
return numOddChars > 0 ? s.length - (numOddChars - 1) : s.length;
19+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [409. Longest Palindrome](https://leetcode.com/problems/longest-palindrome/description)
2+
3+
---
4+
5+
title: "Longest Palindromic Substring in JavaScript"
6+
summary: "An efficient solution to finding the longest palindromic substring in a given string using JavaScript."
7+
date: 2024-06-04
8+
modified_date: 2024-06-04
9+
tags: ["JavaScript", "Algorithm", "Palindrome", "String Manipulation"]
10+
slug: "longest-palindromic-substring-javascript"
11+
12+
---
13+
14+
# Intuition
15+
16+
To find the longest palindromic substring in a given string, the idea is to count the occurrences of each character and determine how many characters have odd counts. This allows us to construct the longest palindrome possible.
17+
18+
# Approach
19+
20+
1. Create a map to count the occurrences of each character in the string.
21+
2. Track the number of characters that have an odd count.
22+
3. Iterate through each character in the string and update the count in the map.
23+
4. Adjust the number of odd-count characters accordingly.
24+
5. Calculate the length of the longest palindromic substring based on the number of odd-count characters.
25+
26+
# Complexity
27+
28+
- **Time complexity:** \(O(n)\) - where \(n\) is the length of the string. We iterate through the string once to count the characters and again to calculate the result.
29+
- **Space complexity:** \(O(1)\) - Since there are a fixed number of possible characters (e.g., 128 ASCII characters), the space complexity is constant.
30+
31+
# Code
32+
33+
```javascript
34+
/**
35+
* @param {string} s
36+
* @return {number}
37+
*/
38+
var longestPalindrome = function (s) {
39+
const charCounts = new Map();
40+
let numOddChars = 0;
41+
42+
for (let char of s) {
43+
const count = charCounts.get(char) || 0;
44+
charCounts.set(char, count + 1);
45+
46+
// Adjust numOddChars based on the new count
47+
numOddChars = (count + 1) % 2 === 1 ? numOddChars + 1 : numOddChars - 1;
48+
}
49+
50+
// If there are odd-count characters, we can use all characters minus (numOddChars - 1)
51+
return numOddChars > 0 ? s.length - (numOddChars - 1) : s.length;
52+
};
53+
```

0 commit comments

Comments
 (0)