Skip to content

Commit f5d8b8a

Browse files
rain84yanglbme
andauthored
feat: add ts solution to lc problem: No.409 (#3032)
* feat: add ts solution to lc problem: No.409 * Create Solution2.py * Create Solution2.java * Create Solution2.cpp * Create Solution2.go * Update Solution2.ts * Update README.md * Update README_EN.md * Update README.md --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent a7a4a1b commit f5d8b8a

File tree

7 files changed

+257
-0
lines changed

7 files changed

+257
-0
lines changed

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

+100
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,104 @@ impl Solution {
183183

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

186+
<!-- solution:start -->
187+
188+
### 方法二:位运算 + 计数
189+
190+
我们可以使用一个数组或哈希表 $odd$ 记录字符串 $s$ 中每个字符是否出现奇数次,用一个整型变量 $cnt$ 记录出现奇数次的字符个数。
191+
192+
遍历字符串 $s$,对于每个字符 $c$,将 $odd[c]$ 取反,即 $0 \rightarrow 1$, $1 \rightarrow 0$。如果 $odd[c]$ 由 $0$ 变为 $1$,则 $cnt$ 加一;如果 $odd[c]$ 由 $1$ 变为 $0$,则 $cnt$ 减一。
193+
194+
最后,如果 $cnt$ 大于 $0$,答案为 $n - cnt + 1$,否则答案为 $n$。
195+
196+
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中,$n$ 为字符串 $s$ 的长度,而 $|\Sigma|$ 为字符集大小,在本题中 $|\Sigma| = 128$。
197+
198+
<!-- tabs:start -->
199+
200+
#### Python3
201+
202+
```python
203+
class Solution:
204+
def longestPalindrome(self, s: str) -> int:
205+
odd = defaultdict(int)
206+
cnt = 0
207+
for c in s:
208+
odd[c] ^= 1
209+
cnt += 1 if odd[c] else -1
210+
return len(s) - cnt + 1 if cnt else len(s)
211+
```
212+
213+
#### Java
214+
215+
```java
216+
class Solution {
217+
public int longestPalindrome(String s) {
218+
int[] odd = new int[128];
219+
int n = s.length();
220+
int cnt = 0;
221+
for (int i = 0; i < n; ++i) {
222+
odd[s.charAt(i)] ^= 1;
223+
cnt += odd[s.charAt(i)] == 1 ? 1 : -1;
224+
}
225+
return cnt > 0 ? n - cnt + 1 : n;
226+
}
227+
}
228+
```
229+
230+
#### C++
231+
232+
```cpp
233+
class Solution {
234+
public:
235+
int longestPalindrome(string s) {
236+
int odd[128]{};
237+
int n = s.length();
238+
int cnt = 0;
239+
for (char& c : s) {
240+
odd[c] ^= 1;
241+
cnt += odd[c] ? 1 : -1;
242+
}
243+
return cnt ? n - cnt + 1 : n;
244+
}
245+
};
246+
```
247+
248+
#### Go
249+
250+
```go
251+
func longestPalindrome(s string) (ans int) {
252+
odd := [128]int{}
253+
cnt := 0
254+
for _, c := range s {
255+
odd[c] ^= 1
256+
cnt += odd[c]
257+
if odd[c] == 0 {
258+
cnt--
259+
}
260+
}
261+
if cnt > 0 {
262+
return len(s) - cnt + 1
263+
}
264+
return len(s)
265+
}
266+
```
267+
268+
#### TypeScript
269+
270+
```ts
271+
function longestPalindrome(s: string): number {
272+
const odd: Record<string, number> = {};
273+
let cnt = 0;
274+
for (const c of s) {
275+
odd[c] ^= 1;
276+
cnt += odd[c] ? 1 : -1;
277+
}
278+
return cnt ? s.length - cnt + 1 : s.length;
279+
}
280+
```
281+
282+
<!-- tabs:end -->
283+
284+
<!-- solution:end -->
285+
186286
<!-- problem:end -->

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

+100
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,104 @@ impl Solution {
180180

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

183+
<!-- solution:start -->
184+
185+
### Solution 2: Bit Manipulation + Counting
186+
187+
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.
188+
189+
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.
190+
191+
Finally, if $cnt$ is greater than $0$, the answer is $n - cnt + 1$, otherwise, the answer is $n$.
192+
193+
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$.
194+
195+
<!-- tabs:start -->
196+
197+
#### Python3
198+
199+
```python
200+
class Solution:
201+
def longestPalindrome(self, s: str) -> int:
202+
odd = defaultdict(int)
203+
cnt = 0
204+
for c in s:
205+
odd[c] ^= 1
206+
cnt += 1 if odd[c] else -1
207+
return len(s) - cnt + 1 if cnt else len(s)
208+
```
209+
210+
#### Java
211+
212+
```java
213+
class Solution {
214+
public int longestPalindrome(String s) {
215+
int[] odd = new int[128];
216+
int n = s.length();
217+
int cnt = 0;
218+
for (int i = 0; i < n; ++i) {
219+
odd[s.charAt(i)] ^= 1;
220+
cnt += odd[s.charAt(i)] == 1 ? 1 : -1;
221+
}
222+
return cnt > 0 ? n - cnt + 1 : n;
223+
}
224+
}
225+
```
226+
227+
#### C++
228+
229+
```cpp
230+
class Solution {
231+
public:
232+
int longestPalindrome(string s) {
233+
int odd[128]{};
234+
int n = s.length();
235+
int cnt = 0;
236+
for (char& c : s) {
237+
odd[c] ^= 1;
238+
cnt += odd[c] ? 1 : -1;
239+
}
240+
return cnt ? n - cnt + 1 : n;
241+
}
242+
};
243+
```
244+
245+
#### Go
246+
247+
```go
248+
func longestPalindrome(s string) (ans int) {
249+
odd := [128]int{}
250+
cnt := 0
251+
for _, c := range s {
252+
odd[c] ^= 1
253+
cnt += odd[c]
254+
if odd[c] == 0 {
255+
cnt--
256+
}
257+
}
258+
if cnt > 0 {
259+
return len(s) - cnt + 1
260+
}
261+
return len(s)
262+
}
263+
```
264+
265+
#### TypeScript
266+
267+
```ts
268+
function longestPalindrome(s: string): number {
269+
const odd: Record<string, number> = {};
270+
let cnt = 0;
271+
for (const c of s) {
272+
odd[c] ^= 1;
273+
cnt += odd[c] ? 1 : -1;
274+
}
275+
return cnt ? s.length - cnt + 1 : s.length;
276+
}
277+
```
278+
279+
<!-- tabs:end -->
280+
281+
<!-- solution:end -->
282+
183283
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int longestPalindrome(string s) {
4+
int odd[128]{};
5+
int n = s.length();
6+
int cnt = 0;
7+
for (char& c : s) {
8+
odd[c] ^= 1;
9+
cnt += odd[c] ? 1 : -1;
10+
}
11+
return cnt ? n - cnt + 1 : n;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func longestPalindrome(s string) (ans int) {
2+
odd := [128]int{}
3+
cnt := 0
4+
for _, c := range s {
5+
odd[c] ^= 1
6+
cnt += odd[c]
7+
if odd[c] == 0 {
8+
cnt--
9+
}
10+
}
11+
if cnt > 0 {
12+
return len(s) - cnt + 1
13+
}
14+
return len(s)
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int longestPalindrome(String s) {
3+
int[] odd = new int[128];
4+
int n = s.length();
5+
int cnt = 0;
6+
for (int i = 0; i < n; ++i) {
7+
odd[s.charAt(i)] ^= 1;
8+
cnt += odd[s.charAt(i)] == 1 ? 1 : -1;
9+
}
10+
return cnt > 0 ? n - cnt + 1 : n;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def longestPalindrome(self, s: str) -> int:
3+
odd = defaultdict(int)
4+
cnt = 0
5+
for c in s:
6+
odd[c] ^= 1
7+
cnt += 1 if odd[c] else -1
8+
return len(s) - cnt + 1 if cnt else len(s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function longestPalindrome(s: string): number {
2+
const odd: Record<string, number> = {};
3+
let cnt = 0;
4+
for (const c of s) {
5+
odd[c] ^= 1;
6+
cnt += odd[c] ? 1 : -1;
7+
}
8+
return cnt ? s.length - cnt + 1 : s.length;
9+
}

0 commit comments

Comments
 (0)