From 09c0c44cbab72467acf245692f29379a1aa4cf70 Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 4 Jun 2024 21:29:40 +0300 Subject: [PATCH 1/9] feat: add ts solution to lc problem: No.409 --- .../0409.Longest Palindrome/README.md | 26 +++++++++++++++++++ .../0409.Longest Palindrome/README_EN.md | 26 +++++++++++++++++++ .../0409.Longest Palindrome/Solution2.ts | 11 ++++++++ 3 files changed, 63 insertions(+) create mode 100644 solution/0400-0499/0409.Longest Palindrome/Solution2.ts diff --git a/solution/0400-0499/0409.Longest Palindrome/README.md b/solution/0400-0499/0409.Longest Palindrome/README.md index cb7100e471835..9724024acf780 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README.md +++ b/solution/0400-0499/0409.Longest Palindrome/README.md @@ -183,4 +183,30 @@ impl Solution { + + +### Solution 2: Bitwise inversion and odd counting + + + +#### TypeScript + +```ts +function longestPalindrome(s: string): number { + const odd: Record = {}; + let c = 0; + + for (const ch of s) { + odd[ch] ^= 1; + c += odd[ch] ? 1 : -1; + } + + return c ? s.length - c + 1 : s.length; +} +``` + + + + + diff --git a/solution/0400-0499/0409.Longest Palindrome/README_EN.md b/solution/0400-0499/0409.Longest Palindrome/README_EN.md index e4a9170a87ff2..bfd37045e0712 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README_EN.md +++ b/solution/0400-0499/0409.Longest Palindrome/README_EN.md @@ -180,4 +180,30 @@ impl Solution { + + +### Solution 2: Bitwise inversion and odd counting + + + +#### TypeScript + +```ts +function longestPalindrome(s: string): number { + const odd: Record = {}; + let c = 0; + + for (const ch of s) { + odd[ch] ^= 1; + c += odd[ch] ? 1 : -1; + } + + return c ? s.length - c + 1 : s.length; +} +``` + + + + + diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution2.ts b/solution/0400-0499/0409.Longest Palindrome/Solution2.ts new file mode 100644 index 0000000000000..e6dfa96487af9 --- /dev/null +++ b/solution/0400-0499/0409.Longest Palindrome/Solution2.ts @@ -0,0 +1,11 @@ +function longestPalindrome(s: string): number { + const odd: Record = {}; + let c = 0; + + for (const ch of s) { + odd[ch] ^= 1; + c += odd[ch] ? 1 : -1; + } + + return c ? s.length - c + 1 : s.length; +} From bdd7f3fd6fae0fb2ea05045c6f278f0b7675be96 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 07:57:06 +0800 Subject: [PATCH 2/9] Create Solution2.py --- solution/0400-0499/0409.Longest Palindrome/Solution2.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 solution/0400-0499/0409.Longest Palindrome/Solution2.py diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution2.py b/solution/0400-0499/0409.Longest Palindrome/Solution2.py new file mode 100644 index 0000000000000..28d3e572e1901 --- /dev/null +++ b/solution/0400-0499/0409.Longest Palindrome/Solution2.py @@ -0,0 +1,8 @@ +class Solution: + def longestPalindrome(self, s: str) -> int: + odd = defaultdict(int) + cnt = 0 + for c in s: + odd[c] ^= 1 + cnt += 1 if odd[c] else -1 + return len(s) - cnt + 1 if cnt else len(s) From 49f48e85b4fc30eb6001bab1eac50e87a99b9a86 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 07:59:17 +0800 Subject: [PATCH 3/9] Create Solution2.java --- .../0400-0499/0409.Longest Palindrome/Solution2.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 solution/0400-0499/0409.Longest Palindrome/Solution2.java diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution2.java b/solution/0400-0499/0409.Longest Palindrome/Solution2.java new file mode 100644 index 0000000000000..3f26683887b89 --- /dev/null +++ b/solution/0400-0499/0409.Longest Palindrome/Solution2.java @@ -0,0 +1,12 @@ +class Solution { + public int longestPalindrome(String s) { + int[] odd = new int[128]; + int n = s.length(); + int cnt = 0; + for (int i = 0; i < n; ++i) { + odd[s.charAt(i)] ^= 1; + cnt += odd[s.charAt(i)] == 1 ? 1 : -1; + } + return cnt > 0 ? n - cnt + 1 : n; + } +} From 4ec1c43a77d402ac7cdd54c3640883b3a6303f11 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 08:00:27 +0800 Subject: [PATCH 4/9] Create Solution2.cpp --- .../0400-0499/0409.Longest Palindrome/Solution2.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 solution/0400-0499/0409.Longest Palindrome/Solution2.cpp diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution2.cpp b/solution/0400-0499/0409.Longest Palindrome/Solution2.cpp new file mode 100644 index 0000000000000..7389a5d94d56a --- /dev/null +++ b/solution/0400-0499/0409.Longest Palindrome/Solution2.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int longestPalindrome(string s) { + int odd[128]{}; + int n = s.length(); + int cnt = 0; + for (char& c : s) { + odd[c] ^= 1; + cnt += odd[c] ? 1 : -1; + } + return cnt ? n - cnt + 1 : n; + } +}; From c4562d387a29d5711be50c369642eaf3d94e51ac Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 08:02:38 +0800 Subject: [PATCH 5/9] Create Solution2.go --- .../0409.Longest Palindrome/Solution2.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 solution/0400-0499/0409.Longest Palindrome/Solution2.go diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution2.go b/solution/0400-0499/0409.Longest Palindrome/Solution2.go new file mode 100644 index 0000000000000..2031f84d0897e --- /dev/null +++ b/solution/0400-0499/0409.Longest Palindrome/Solution2.go @@ -0,0 +1,15 @@ +func longestPalindrome(s string) (ans int) { + odd := [128]int{} + cnt := 0 + for _, c := range s { + odd[c] ^= 1 + cnt += odd[c] + if odd[c] == 0 { + cnt-- + } + } + if cnt > 0 { + return len(s) - cnt + 1 + } + return len(s) +} From 02880975d38a5a585219dfddbfb0b0cd3d0260a8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 08:03:36 +0800 Subject: [PATCH 6/9] Update Solution2.ts --- .../0400-0499/0409.Longest Palindrome/Solution2.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/solution/0400-0499/0409.Longest Palindrome/Solution2.ts b/solution/0400-0499/0409.Longest Palindrome/Solution2.ts index e6dfa96487af9..10ae8b4d79fdb 100644 --- a/solution/0400-0499/0409.Longest Palindrome/Solution2.ts +++ b/solution/0400-0499/0409.Longest Palindrome/Solution2.ts @@ -1,11 +1,9 @@ function longestPalindrome(s: string): number { const odd: Record = {}; - let c = 0; - - for (const ch of s) { - odd[ch] ^= 1; - c += odd[ch] ? 1 : -1; + let cnt = 0; + for (const c of s) { + odd[c] ^= 1; + cnt += odd[c] ? 1 : -1; } - - return c ? s.length - c + 1 : s.length; + return cnt ? s.length - cnt + 1 : s.length; } From 61def1a94446c11a2973209408cdbd7469f01eaf Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 08:09:31 +0800 Subject: [PATCH 7/9] Update README.md --- .../0409.Longest Palindrome/README.md | 90 +++++++++++++++++-- 1 file changed, 82 insertions(+), 8 deletions(-) diff --git a/solution/0400-0499/0409.Longest Palindrome/README.md b/solution/0400-0499/0409.Longest Palindrome/README.md index 9724024acf780..65d3e53fc7a9e 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README.md +++ b/solution/0400-0499/0409.Longest Palindrome/README.md @@ -185,23 +185,97 @@ impl Solution { -### Solution 2: Bitwise inversion and odd counting +### Solution 2: Bit Manipulation + Counting + +We can use an array or hash table $odd$ to record whether each character in string $s$ appears an odd number of times, and an integer variable $cnt$ to record the number of characters that appear an odd number of times. + +We iterate through the string $s$. For each character $c$, we flip $odd[c]$, i.e., $0 \rightarrow 1$, $1 \rightarrow 0$. If $odd[c]$ changes from $0$ to $1$, then we increment $cnt$ by one; if $odd[c]$ changes from $1$ to $0$, then we decrement $cnt$ by one. + +Finally, if $cnt$ is greater than $0$, the answer is $n - cnt + 1$, otherwise, the answer is $n$. + +The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string $s$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| = 128$. +#### Python3 + +```python +class Solution: + def longestPalindrome(self, s: str) -> int: + odd = defaultdict(int) + cnt = 0 + for c in s: + odd[c] ^= 1 + cnt += 1 if odd[c] else -1 + return len(s) - cnt + 1 if cnt else len(s) +``` + +#### Java + +```java +class Solution { + public int longestPalindrome(String s) { + int[] odd = new int[128]; + int n = s.length(); + int cnt = 0; + for (int i = 0; i < n; ++i) { + odd[s.charAt(i)] ^= 1; + cnt += odd[s.charAt(i)] == 1 ? 1 : -1; + } + return cnt > 0 ? n - cnt + 1 : n; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int longestPalindrome(string s) { + int odd[128]{}; + int n = s.length(); + int cnt = 0; + for (char& c : s) { + odd[c] ^= 1; + cnt += odd[c] ? 1 : -1; + } + return cnt ? n - cnt + 1 : n; + } +}; +``` + +#### Go + +```go +func longestPalindrome(s string) (ans int) { + odd := [128]int{} + cnt := 0 + for _, c := range s { + odd[c] ^= 1 + cnt += odd[c] + if odd[c] == 0 { + cnt-- + } + } + if cnt > 0 { + return len(s) - cnt + 1 + } + return len(s) +} +``` + #### TypeScript ```ts function longestPalindrome(s: string): number { const odd: Record = {}; - let c = 0; - - for (const ch of s) { - odd[ch] ^= 1; - c += odd[ch] ? 1 : -1; + let cnt = 0; + for (const c of s) { + odd[c] ^= 1; + cnt += odd[c] ? 1 : -1; } - - return c ? s.length - c + 1 : s.length; + return cnt ? s.length - cnt + 1 : s.length; } ``` From e3d3427d2eb17ce95bed813b3251622e8b6a4e82 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 08:09:56 +0800 Subject: [PATCH 8/9] Update README_EN.md --- .../0409.Longest Palindrome/README_EN.md | 90 +++++++++++++++++-- 1 file changed, 82 insertions(+), 8 deletions(-) diff --git a/solution/0400-0499/0409.Longest Palindrome/README_EN.md b/solution/0400-0499/0409.Longest Palindrome/README_EN.md index bfd37045e0712..076e5ff020358 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README_EN.md +++ b/solution/0400-0499/0409.Longest Palindrome/README_EN.md @@ -182,23 +182,97 @@ impl Solution { -### Solution 2: Bitwise inversion and odd counting +### Solution 2: Bit Manipulation + Counting + +We can use an array or hash table $odd$ to record whether each character in string $s$ appears an odd number of times, and an integer variable $cnt$ to record the number of characters that appear an odd number of times. + +We iterate through the string $s$. For each character $c$, we flip $odd[c]$, i.e., $0 \rightarrow 1$, $1 \rightarrow 0$. If $odd[c]$ changes from $0$ to $1$, then we increment $cnt$ by one; if $odd[c]$ changes from $1$ to $0$, then we decrement $cnt$ by one. + +Finally, if $cnt$ is greater than $0$, the answer is $n - cnt + 1$, otherwise, the answer is $n$. + +The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string $s$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| = 128$. +#### Python3 + +```python +class Solution: + def longestPalindrome(self, s: str) -> int: + odd = defaultdict(int) + cnt = 0 + for c in s: + odd[c] ^= 1 + cnt += 1 if odd[c] else -1 + return len(s) - cnt + 1 if cnt else len(s) +``` + +#### Java + +```java +class Solution { + public int longestPalindrome(String s) { + int[] odd = new int[128]; + int n = s.length(); + int cnt = 0; + for (int i = 0; i < n; ++i) { + odd[s.charAt(i)] ^= 1; + cnt += odd[s.charAt(i)] == 1 ? 1 : -1; + } + return cnt > 0 ? n - cnt + 1 : n; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int longestPalindrome(string s) { + int odd[128]{}; + int n = s.length(); + int cnt = 0; + for (char& c : s) { + odd[c] ^= 1; + cnt += odd[c] ? 1 : -1; + } + return cnt ? n - cnt + 1 : n; + } +}; +``` + +#### Go + +```go +func longestPalindrome(s string) (ans int) { + odd := [128]int{} + cnt := 0 + for _, c := range s { + odd[c] ^= 1 + cnt += odd[c] + if odd[c] == 0 { + cnt-- + } + } + if cnt > 0 { + return len(s) - cnt + 1 + } + return len(s) +} +``` + #### TypeScript ```ts function longestPalindrome(s: string): number { const odd: Record = {}; - let c = 0; - - for (const ch of s) { - odd[ch] ^= 1; - c += odd[ch] ? 1 : -1; + let cnt = 0; + for (const c of s) { + odd[c] ^= 1; + cnt += odd[c] ? 1 : -1; } - - return c ? s.length - c + 1 : s.length; + return cnt ? s.length - cnt + 1 : s.length; } ``` From a8f84e683f4cf68cf19c1bfc3a3ee8fa1d74f76d Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 5 Jun 2024 08:10:18 +0800 Subject: [PATCH 9/9] Update README.md --- solution/0400-0499/0409.Longest Palindrome/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/solution/0400-0499/0409.Longest Palindrome/README.md b/solution/0400-0499/0409.Longest Palindrome/README.md index 65d3e53fc7a9e..1da632a01076c 100644 --- a/solution/0400-0499/0409.Longest Palindrome/README.md +++ b/solution/0400-0499/0409.Longest Palindrome/README.md @@ -185,15 +185,15 @@ impl Solution { -### Solution 2: Bit Manipulation + Counting +### 方法二:位运算 + 计数 -We can use an array or hash table $odd$ to record whether each character in string $s$ appears an odd number of times, and an integer variable $cnt$ to record the number of characters that appear an odd number of times. +我们可以使用一个数组或哈希表 $odd$ 记录字符串 $s$ 中每个字符是否出现奇数次,用一个整型变量 $cnt$ 记录出现奇数次的字符个数。 -We iterate through the string $s$. For each character $c$, we flip $odd[c]$, i.e., $0 \rightarrow 1$, $1 \rightarrow 0$. If $odd[c]$ changes from $0$ to $1$, then we increment $cnt$ by one; if $odd[c]$ changes from $1$ to $0$, then we decrement $cnt$ by one. +遍历字符串 $s$,对于每个字符 $c$,将 $odd[c]$ 取反,即 $0 \rightarrow 1$, $1 \rightarrow 0$。如果 $odd[c]$ 由 $0$ 变为 $1$,则 $cnt$ 加一;如果 $odd[c]$ 由 $1$ 变为 $0$,则 $cnt$ 减一。 -Finally, if $cnt$ is greater than $0$, the answer is $n - cnt + 1$, otherwise, the answer is $n$. +最后,如果 $cnt$ 大于 $0$,答案为 $n - cnt + 1$,否则答案为 $n$。 -The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string $s$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| = 128$. +时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中,$n$ 为字符串 $s$ 的长度,而 $|\Sigma|$ 为字符集大小,在本题中 $|\Sigma| = 128$。