Skip to content

feat: add ts solution to lc problem: No.409 #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions solution/0400-0499/0409.Longest Palindrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,104 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:位运算 + 计数

我们可以使用一个数组或哈希表 $odd$ 记录字符串 $s$ 中每个字符是否出现奇数次,用一个整型变量 $cnt$ 记录出现奇数次的字符个数。

遍历字符串 $s$,对于每个字符 $c$,将 $odd[c]$ 取反,即 $0 \rightarrow 1$, $1 \rightarrow 0$。如果 $odd[c]$ 由 $0$ 变为 $1$,则 $cnt$ 加一;如果 $odd[c]$ 由 $1$ 变为 $0$,则 $cnt$ 减一。

最后,如果 $cnt$ 大于 $0$,答案为 $n - cnt + 1$,否则答案为 $n$。

时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中,$n$ 为字符串 $s$ 的长度,而 $|\Sigma|$ 为字符集大小,在本题中 $|\Sigma| = 128$。

<!-- tabs:start -->

#### 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<string, number> = {};
let cnt = 0;
for (const c of s) {
odd[c] ^= 1;
cnt += odd[c] ? 1 : -1;
}
return cnt ? s.length - cnt + 1 : s.length;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
100 changes: 100 additions & 0 deletions solution/0400-0499/0409.Longest Palindrome/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,104 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

#### 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<string, number> = {};
let cnt = 0;
for (const c of s) {
odd[c] ^= 1;
cnt += odd[c] ? 1 : -1;
}
return cnt ? s.length - cnt + 1 : s.length;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
13 changes: 13 additions & 0 deletions solution/0400-0499/0409.Longest Palindrome/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
15 changes: 15 additions & 0 deletions solution/0400-0499/0409.Longest Palindrome/Solution2.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 12 additions & 0 deletions solution/0400-0499/0409.Longest Palindrome/Solution2.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions solution/0400-0499/0409.Longest Palindrome/Solution2.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions solution/0400-0499/0409.Longest Palindrome/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function longestPalindrome(s: string): number {
const odd: Record<string, number> = {};
let cnt = 0;
for (const c of s) {
odd[c] ^= 1;
cnt += odd[c] ? 1 : -1;
}
return cnt ? s.length - cnt + 1 : s.length;
}
Loading