diff --git a/solution/0400-0499/0409.Longest Palindrome/README.md b/solution/0400-0499/0409.Longest Palindrome/README.md index 280e44f92119c..abb7f7858579f 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README.md +++ b/solution/0400-0499/0409.Longest Palindrome/README.md @@ -70,6 +70,24 @@ class Solution { } ``` +### **TypeScript** + +```ts +function longestPalindrome(s: string): number { + let n = s.length; + let ans = 0; + let record = new Array(128).fill(0); + for (let i = 0; i < n; i++) { + record[s.charCodeAt(i)]++; + } + for (let i = 65; i < 128; i++) { + let count = record[i]; + ans += (count % 2 == 0 ? count : count - 1); + } + return ans < s.length ? ans + 1 : ans; +}; +``` + ### **...** ``` diff --git a/solution/0400-0499/0409.Longest Palindrome/README_EN.md b/solution/0400-0499/0409.Longest Palindrome/README_EN.md index ae14d3d179e74..a8f844202d39d 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README_EN.md +++ b/solution/0400-0499/0409.Longest Palindrome/README_EN.md @@ -78,6 +78,24 @@ class Solution { } ``` +### **TypeScript** + +```ts +function longestPalindrome(s: string): number { + let n = s.length; + let ans = 0; + let record = new Array(128).fill(0); + for (let i = 0; i < n; i++) { + record[s.charCodeAt(i)]++; + } + for (let i = 65; i < 128; i++) { + let count = record[i]; + ans += (count % 2 == 0 ? count : count - 1); + } + return ans < s.length ? ans + 1 : ans; +}; +``` + ### **...** ``` diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution.ts b/solution/0400-0499/0409.Longest Palindrome/Solution.ts new file mode 100644 index 0000000000000..991a886ffafe1 --- /dev/null +++ b/solution/0400-0499/0409.Longest Palindrome/Solution.ts @@ -0,0 +1,13 @@ +function longestPalindrome(s: string): number { + let n = s.length; + let ans = 0; + let record = new Array(128).fill(0); + for (let i = 0; i < n; i++) { + record[s.charCodeAt(i)]++; + } + for (let i = 65; i < 128; i++) { + let count = record[i]; + ans += (count % 2 == 0 ? count : count - 1); + } + return ans < s.length ? ans + 1 : ans; +}; \ No newline at end of file