Skip to content
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

feat: add solutions to lc problems: No.0266,2525~2527 #1505

Merged
merged 1 commit into from
Aug 25, 2023
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
60 changes: 36 additions & 24 deletions solution/0200-0299/0266.Palindrome Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@

创建一个长度为 $26$ 的数组,统计每个字母出现的频率,至多有一个字符出现奇数次数即可。

时间复杂度 $O(n)$,空间复杂度 $O(26)$。其中 $n$ 是字符串的长度。
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma|=26$

**方法二:哈希表**

利用哈希表来维护元素。遍历字符串每个字母 $s[i]$,若 $s[i]$ 在哈希表中,则将 $s[i]$ 从哈希表中删除,否则将 $s[i]$ 加入哈希表。

遍历结束,若哈希表中元素个数不超过 $1$,则返回 $true$,否则返回 $false$。

时间复杂度 $O(n)$,空间复杂度 $O(26)$。其中 $n$ 是字符串的长度。
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma|=26$

<!-- tabs:start -->

Expand All @@ -50,7 +50,7 @@
```python
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
return sum(v % 2 for v in Counter(s).values()) <= 1
return sum(v & 1 for v in Counter(s).values()) < 2
```

### **Java**
Expand All @@ -64,11 +64,11 @@ class Solution {
for (char c : s.toCharArray()) {
++cnt[c - 'a'];
}
int n = 0;
for (int v : cnt) {
n += v % 2;
int odd = 0;
for (int x : cnt) {
odd += x & 1;
}
return n < 2;
return odd < 2;
}
}
```
Expand All @@ -80,10 +80,14 @@ class Solution {
public:
bool canPermutePalindrome(string s) {
vector<int> cnt(26);
for (char& c : s) ++cnt[c - 'a'];
int n = 0;
for (int& v : cnt) n += v & 1;
return n < 2;
for (char& c : s) {
++cnt[c - 'a'];
}
int odd = 0;
for (int x : cnt) {
odd += x & 1;
}
return odd < 2;
}
};
```
Expand All @@ -92,15 +96,27 @@ public:

```go
func canPermutePalindrome(s string) bool {
cnt := make([]int, 26)
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
n := 0
for _, v := range cnt {
n += v & 1
odd := 0
for _, x := range cnt {
odd += x & 1
}
return n < 2
return odd < 2
}
```

### **TypeScript**

```ts
function canPermutePalindrome(s: string): boolean {
const cnt: number[] = new Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt(0) - 97];
}
return cnt.filter(c => c % 2 === 1).length < 2;
}
```

Expand All @@ -112,15 +128,11 @@ func canPermutePalindrome(s string) bool {
* @return {boolean}
*/
var canPermutePalindrome = function (s) {
let ss = new Set();
for (let c of s) {
if (ss.has(c)) {
ss.delete(c);
} else {
ss.add(c);
}
const cnt = new Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
}
return ss.size < 2;
return cnt.filter(c => c % 2 === 1).length < 2;
};
```

Expand Down
56 changes: 34 additions & 22 deletions solution/0200-0299/0266.Palindrome Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
```python
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
return sum(v % 2 for v in Counter(s).values()) <= 1
return sum(v & 1 for v in Counter(s).values()) < 2
```

### **Java**
Expand All @@ -57,11 +57,11 @@ class Solution {
for (char c : s.toCharArray()) {
++cnt[c - 'a'];
}
int n = 0;
for (int v : cnt) {
n += v % 2;
int odd = 0;
for (int x : cnt) {
odd += x & 1;
}
return n < 2;
return odd < 2;
}
}
```
Expand All @@ -73,10 +73,14 @@ class Solution {
public:
bool canPermutePalindrome(string s) {
vector<int> cnt(26);
for (char& c : s) ++cnt[c - 'a'];
int n = 0;
for (int& v : cnt) n += v & 1;
return n < 2;
for (char& c : s) {
++cnt[c - 'a'];
}
int odd = 0;
for (int x : cnt) {
odd += x & 1;
}
return odd < 2;
}
};
```
Expand All @@ -85,15 +89,27 @@ public:

```go
func canPermutePalindrome(s string) bool {
cnt := make([]int, 26)
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
n := 0
for _, v := range cnt {
n += v & 1
odd := 0
for _, x := range cnt {
odd += x & 1
}
return n < 2
return odd < 2
}
```

### **TypeScript**

```ts
function canPermutePalindrome(s: string): boolean {
const cnt: number[] = new Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt(0) - 97];
}
return cnt.filter(c => c % 2 === 1).length < 2;
}
```

Expand All @@ -105,15 +121,11 @@ func canPermutePalindrome(s string) bool {
* @return {boolean}
*/
var canPermutePalindrome = function (s) {
let ss = new Set();
for (let c of s) {
if (ss.has(c)) {
ss.delete(c);
} else {
ss.add(c);
}
const cnt = new Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
}
return ss.size < 2;
return cnt.filter(c => c % 2 === 1).length < 2;
};
```

Expand Down
22 changes: 13 additions & 9 deletions solution/0200-0299/0266.Palindrome Permutation/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
class Solution {
public:
bool canPermutePalindrome(string s) {
vector<int> cnt(26);
for (char& c : s) ++cnt[c - 'a'];
int n = 0;
for (int& v : cnt) n += v & 1;
return n < 2;
}
class Solution {
public:
bool canPermutePalindrome(string s) {
vector<int> cnt(26);
for (char& c : s) {
++cnt[c - 'a'];
}
int odd = 0;
for (int x : cnt) {
odd += x & 1;
}
return odd < 2;
}
};
10 changes: 5 additions & 5 deletions solution/0200-0299/0266.Palindrome Permutation/Solution.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
func canPermutePalindrome(s string) bool {
cnt := make([]int, 26)
cnt := [26]int{}
for _, c := range s {
cnt[c-'a']++
}
n := 0
for _, v := range cnt {
n += v & 1
odd := 0
for _, x := range cnt {
odd += x & 1
}
return n < 2
return odd < 2
}
24 changes: 12 additions & 12 deletions solution/0200-0299/0266.Palindrome Permutation/Solution.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Solution {
public boolean canPermutePalindrome(String s) {
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
++cnt[c - 'a'];
}
int n = 0;
for (int v : cnt) {
n += v % 2;
}
return n < 2;
}
class Solution {
public boolean canPermutePalindrome(String s) {
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
++cnt[c - 'a'];
}
int odd = 0;
for (int x : cnt) {
odd += x & 1;
}
return odd < 2;
}
}
12 changes: 4 additions & 8 deletions solution/0200-0299/0266.Palindrome Permutation/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
* @return {boolean}
*/
var canPermutePalindrome = function (s) {
let ss = new Set();
for (let c of s) {
if (ss.has(c)) {
ss.delete(c);
} else {
ss.add(c);
}
const cnt = new Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
}
return ss.size < 2;
return cnt.filter(c => c % 2 === 1).length < 2;
};
6 changes: 3 additions & 3 deletions solution/0200-0299/0266.Palindrome Permutation/Solution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
return sum(v % 2 for v in Counter(s).values()) <= 1
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
return sum(v & 1 for v in Counter(s).values()) < 2
7 changes: 7 additions & 0 deletions solution/0200-0299/0266.Palindrome Permutation/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function canPermutePalindrome(s: string): boolean {
const cnt: number[] = new Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt(0) - 97];
}
return cnt.filter(c => c % 2 === 1).length < 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,32 @@ func categorizeBox(length int, width int, height int, mass int) string {
}
```

### **TypeScript**

```ts
function categorizeBox(
length: number,
width: number,
height: number,
mass: number,
): string {
const v = length * width * height;
let i = 0;
if (
length >= 10000 ||
width >= 10000 ||
height >= 10000 ||
v >= 1000000000
) {
i |= 1;
}
if (mass >= 100) {
i |= 2;
}
return ['Neither', 'Bulky', 'Heavy', 'Both'][i];
}
```

### **Rust**

```rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,32 @@ func categorizeBox(length int, width int, height int, mass int) string {
}
```

### **TypeScript**

```ts
function categorizeBox(
length: number,
width: number,
height: number,
mass: number,
): string {
const v = length * width * height;
let i = 0;
if (
length >= 10000 ||
width >= 10000 ||
height >= 10000 ||
v >= 1000000000
) {
i |= 1;
}
if (mass >= 100) {
i |= 2;
}
return ['Neither', 'Bulky', 'Heavy', 'Both'][i];
}
```

### **Rust**

```rust
Expand Down
Loading