Skip to content

Commit 09c0c44

Browse files
committed
feat: add ts solution to lc problem: No.409
1 parent a7a4a1b commit 09c0c44

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

solution/0400-0499/0409.Longest Palindrome/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,30 @@ impl Solution {
183183

184184
<!-- solution:end -->
185185

186+
<!-- solution:start -->
187+
188+
### Solution 2: Bitwise inversion and odd counting
189+
190+
<!-- tabs:start -->
191+
192+
#### TypeScript
193+
194+
```ts
195+
function longestPalindrome(s: string): number {
196+
const odd: Record<string, number> = {};
197+
let c = 0;
198+
199+
for (const ch of s) {
200+
odd[ch] ^= 1;
201+
c += odd[ch] ? 1 : -1;
202+
}
203+
204+
return c ? s.length - c + 1 : s.length;
205+
}
206+
```
207+
208+
<!-- tabs:end -->
209+
210+
<!-- solution:end -->
211+
186212
<!-- problem:end -->

solution/0400-0499/0409.Longest Palindrome/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,30 @@ impl Solution {
180180

181181
<!-- solution:end -->
182182

183+
<!-- solution:start -->
184+
185+
### Solution 2: Bitwise inversion and odd counting
186+
187+
<!-- tabs:start -->
188+
189+
#### TypeScript
190+
191+
```ts
192+
function longestPalindrome(s: string): number {
193+
const odd: Record<string, number> = {};
194+
let c = 0;
195+
196+
for (const ch of s) {
197+
odd[ch] ^= 1;
198+
c += odd[ch] ? 1 : -1;
199+
}
200+
201+
return c ? s.length - c + 1 : s.length;
202+
}
203+
```
204+
205+
<!-- tabs:end -->
206+
207+
<!-- solution:end -->
208+
183209
<!-- problem:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function longestPalindrome(s: string): number {
2+
const odd: Record<string, number> = {};
3+
let c = 0;
4+
5+
for (const ch of s) {
6+
odd[ch] ^= 1;
7+
c += odd[ch] ? 1 : -1;
8+
}
9+
10+
return c ? s.length - c + 1 : s.length;
11+
}

0 commit comments

Comments
 (0)