Skip to content

Commit cbc7f08

Browse files
authoredAug 25, 2023
feat: add solutions to lc problems: No.0266,2525~2527 (#1505)
* No.0266.Palindrome Permutation * No.2525.Categorize Box According to Criteria * No.2526.Find Consecutive Integers from a Data Stream * No.2527.Find Xor-Beauty of Array
1 parent 4ea153a commit cbc7f08

File tree

17 files changed

+282
-83
lines changed

17 files changed

+282
-83
lines changed
 

‎solution/0200-0299/0266.Palindrome Permutation/README.md

+36-24
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131

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

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

3636
**方法二:哈希表**
3737

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

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

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

4444
<!-- tabs:start -->
4545

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

5656
### **Java**
@@ -64,11 +64,11 @@ class Solution {
6464
for (char c : s.toCharArray()) {
6565
++cnt[c - 'a'];
6666
}
67-
int n = 0;
68-
for (int v : cnt) {
69-
n += v % 2;
67+
int odd = 0;
68+
for (int x : cnt) {
69+
odd += x & 1;
7070
}
71-
return n < 2;
71+
return odd < 2;
7272
}
7373
}
7474
```
@@ -80,10 +80,14 @@ class Solution {
8080
public:
8181
bool canPermutePalindrome(string s) {
8282
vector<int> cnt(26);
83-
for (char& c : s) ++cnt[c - 'a'];
84-
int n = 0;
85-
for (int& v : cnt) n += v & 1;
86-
return n < 2;
83+
for (char& c : s) {
84+
++cnt[c - 'a'];
85+
}
86+
int odd = 0;
87+
for (int x : cnt) {
88+
odd += x & 1;
89+
}
90+
return odd < 2;
8791
}
8892
};
8993
```
@@ -92,15 +96,27 @@ public:
9296
9397
```go
9498
func canPermutePalindrome(s string) bool {
95-
cnt := make([]int, 26)
99+
cnt := [26]int{}
96100
for _, c := range s {
97101
cnt[c-'a']++
98102
}
99-
n := 0
100-
for _, v := range cnt {
101-
n += v & 1
103+
odd := 0
104+
for _, x := range cnt {
105+
odd += x & 1
102106
}
103-
return n < 2
107+
return odd < 2
108+
}
109+
```
110+
111+
### **TypeScript**
112+
113+
```ts
114+
function canPermutePalindrome(s: string): boolean {
115+
const cnt: number[] = new Array(26).fill(0);
116+
for (const c of s) {
117+
++cnt[c.charCodeAt(0) - 97];
118+
}
119+
return cnt.filter(c => c % 2 === 1).length < 2;
104120
}
105121
```
106122

@@ -112,15 +128,11 @@ func canPermutePalindrome(s string) bool {
112128
* @return {boolean}
113129
*/
114130
var canPermutePalindrome = function (s) {
115-
let ss = new Set();
116-
for (let c of s) {
117-
if (ss.has(c)) {
118-
ss.delete(c);
119-
} else {
120-
ss.add(c);
121-
}
131+
const cnt = new Array(26).fill(0);
132+
for (const c of s) {
133+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
122134
}
123-
return ss.size < 2;
135+
return cnt.filter(c => c % 2 === 1).length < 2;
124136
};
125137
```
126138

‎solution/0200-0299/0266.Palindrome Permutation/README_EN.md

+34-22
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
```python
4646
class Solution:
4747
def canPermutePalindrome(self, s: str) -> bool:
48-
return sum(v % 2 for v in Counter(s).values()) <= 1
48+
return sum(v & 1 for v in Counter(s).values()) < 2
4949
```
5050

5151
### **Java**
@@ -57,11 +57,11 @@ class Solution {
5757
for (char c : s.toCharArray()) {
5858
++cnt[c - 'a'];
5959
}
60-
int n = 0;
61-
for (int v : cnt) {
62-
n += v % 2;
60+
int odd = 0;
61+
for (int x : cnt) {
62+
odd += x & 1;
6363
}
64-
return n < 2;
64+
return odd < 2;
6565
}
6666
}
6767
```
@@ -73,10 +73,14 @@ class Solution {
7373
public:
7474
bool canPermutePalindrome(string s) {
7575
vector<int> cnt(26);
76-
for (char& c : s) ++cnt[c - 'a'];
77-
int n = 0;
78-
for (int& v : cnt) n += v & 1;
79-
return n < 2;
76+
for (char& c : s) {
77+
++cnt[c - 'a'];
78+
}
79+
int odd = 0;
80+
for (int x : cnt) {
81+
odd += x & 1;
82+
}
83+
return odd < 2;
8084
}
8185
};
8286
```
@@ -85,15 +89,27 @@ public:
8589
8690
```go
8791
func canPermutePalindrome(s string) bool {
88-
cnt := make([]int, 26)
92+
cnt := [26]int{}
8993
for _, c := range s {
9094
cnt[c-'a']++
9195
}
92-
n := 0
93-
for _, v := range cnt {
94-
n += v & 1
96+
odd := 0
97+
for _, x := range cnt {
98+
odd += x & 1
9599
}
96-
return n < 2
100+
return odd < 2
101+
}
102+
```
103+
104+
### **TypeScript**
105+
106+
```ts
107+
function canPermutePalindrome(s: string): boolean {
108+
const cnt: number[] = new Array(26).fill(0);
109+
for (const c of s) {
110+
++cnt[c.charCodeAt(0) - 97];
111+
}
112+
return cnt.filter(c => c % 2 === 1).length < 2;
97113
}
98114
```
99115

@@ -105,15 +121,11 @@ func canPermutePalindrome(s string) bool {
105121
* @return {boolean}
106122
*/
107123
var canPermutePalindrome = function (s) {
108-
let ss = new Set();
109-
for (let c of s) {
110-
if (ss.has(c)) {
111-
ss.delete(c);
112-
} else {
113-
ss.add(c);
114-
}
124+
const cnt = new Array(26).fill(0);
125+
for (const c of s) {
126+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
115127
}
116-
return ss.size < 2;
128+
return cnt.filter(c => c % 2 === 1).length < 2;
117129
};
118130
```
119131

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
class Solution {
2-
public:
3-
bool canPermutePalindrome(string s) {
4-
vector<int> cnt(26);
5-
for (char& c : s) ++cnt[c - 'a'];
6-
int n = 0;
7-
for (int& v : cnt) n += v & 1;
8-
return n < 2;
9-
}
1+
class Solution {
2+
public:
3+
bool canPermutePalindrome(string s) {
4+
vector<int> cnt(26);
5+
for (char& c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
int odd = 0;
9+
for (int x : cnt) {
10+
odd += x & 1;
11+
}
12+
return odd < 2;
13+
}
1014
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
func canPermutePalindrome(s string) bool {
2-
cnt := make([]int, 26)
2+
cnt := [26]int{}
33
for _, c := range s {
44
cnt[c-'a']++
55
}
6-
n := 0
7-
for _, v := range cnt {
8-
n += v & 1
6+
odd := 0
7+
for _, x := range cnt {
8+
odd += x & 1
99
}
10-
return n < 2
10+
return odd < 2
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
class Solution {
2-
public boolean canPermutePalindrome(String s) {
3-
int[] cnt = new int[26];
4-
for (char c : s.toCharArray()) {
5-
++cnt[c - 'a'];
6-
}
7-
int n = 0;
8-
for (int v : cnt) {
9-
n += v % 2;
10-
}
11-
return n < 2;
12-
}
1+
class Solution {
2+
public boolean canPermutePalindrome(String s) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++cnt[c - 'a'];
6+
}
7+
int odd = 0;
8+
for (int x : cnt) {
9+
odd += x & 1;
10+
}
11+
return odd < 2;
12+
}
1313
}

‎solution/0200-0299/0266.Palindrome Permutation/Solution.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
* @return {boolean}
44
*/
55
var canPermutePalindrome = function (s) {
6-
let ss = new Set();
7-
for (let c of s) {
8-
if (ss.has(c)) {
9-
ss.delete(c);
10-
} else {
11-
ss.add(c);
12-
}
6+
const cnt = new Array(26).fill(0);
7+
for (const c of s) {
8+
++cnt[c.charCodeAt() - 'a'.charCodeAt()];
139
}
14-
return ss.size < 2;
10+
return cnt.filter(c => c % 2 === 1).length < 2;
1511
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
class Solution:
2-
def canPermutePalindrome(self, s: str) -> bool:
3-
return sum(v % 2 for v in Counter(s).values()) <= 1
1+
class Solution:
2+
def canPermutePalindrome(self, s: str) -> bool:
3+
return sum(v & 1 for v in Counter(s).values()) < 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function canPermutePalindrome(s: string): boolean {
2+
const cnt: number[] = new Array(26).fill(0);
3+
for (const c of s) {
4+
++cnt[c.charCodeAt(0) - 97];
5+
}
6+
return cnt.filter(c => c % 2 === 1).length < 2;
7+
}

‎solution/2500-2599/2525.Categorize Box According to Criteria/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,32 @@ func categorizeBox(length int, width int, height int, mass int) string {
134134
}
135135
```
136136

137+
### **TypeScript**
138+
139+
```ts
140+
function categorizeBox(
141+
length: number,
142+
width: number,
143+
height: number,
144+
mass: number,
145+
): string {
146+
const v = length * width * height;
147+
let i = 0;
148+
if (
149+
length >= 10000 ||
150+
width >= 10000 ||
151+
height >= 10000 ||
152+
v >= 1000000000
153+
) {
154+
i |= 1;
155+
}
156+
if (mass >= 100) {
157+
i |= 2;
158+
}
159+
return ['Neither', 'Bulky', 'Heavy', 'Both'][i];
160+
}
161+
```
162+
137163
### **Rust**
138164

139165
```rust

‎solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,32 @@ func categorizeBox(length int, width int, height int, mass int) string {
118118
}
119119
```
120120

121+
### **TypeScript**
122+
123+
```ts
124+
function categorizeBox(
125+
length: number,
126+
width: number,
127+
height: number,
128+
mass: number,
129+
): string {
130+
const v = length * width * height;
131+
let i = 0;
132+
if (
133+
length >= 10000 ||
134+
width >= 10000 ||
135+
height >= 10000 ||
136+
v >= 1000000000
137+
) {
138+
i |= 1;
139+
}
140+
if (mass >= 100) {
141+
i |= 2;
142+
}
143+
return ['Neither', 'Bulky', 'Heavy', 'Both'][i];
144+
}
145+
```
146+
121147
### **Rust**
122148

123149
```rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function categorizeBox(
2+
length: number,
3+
width: number,
4+
height: number,
5+
mass: number,
6+
): string {
7+
const v = length * width * height;
8+
let i = 0;
9+
if (
10+
length >= 10000 ||
11+
width >= 10000 ||
12+
height >= 10000 ||
13+
v >= 1000000000
14+
) {
15+
i |= 1;
16+
}
17+
if (mass >= 100) {
18+
i |= 2;
19+
}
20+
return ['Neither', 'Bulky', 'Heavy', 'Both'][i];
21+
}

‎solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,33 @@ func (this *DataStream) Consec(num int) bool {
162162
*/
163163
```
164164

165+
### **TypeScript**
166+
167+
```ts
168+
class DataStream {
169+
private val: number;
170+
private k: number;
171+
private cnt: number;
172+
173+
constructor(value: number, k: number) {
174+
this.val = value;
175+
this.k = k;
176+
this.cnt = 0;
177+
}
178+
179+
consec(num: number): boolean {
180+
this.cnt = this.val === num ? this.cnt + 1 : 0;
181+
return this.cnt >= this.k;
182+
}
183+
}
184+
185+
/**
186+
* Your DataStream object will be instantiated and called as such:
187+
* var obj = new DataStream(value, k)
188+
* var param_1 = obj.consec(num)
189+
*/
190+
```
191+
165192
### **...**
166193

167194
```

‎solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md

+27
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,33 @@ func (this *DataStream) Consec(num int) bool {
144144
*/
145145
```
146146

147+
### **TypeScript**
148+
149+
```ts
150+
class DataStream {
151+
private val: number;
152+
private k: number;
153+
private cnt: number;
154+
155+
constructor(value: number, k: number) {
156+
this.val = value;
157+
this.k = k;
158+
this.cnt = 0;
159+
}
160+
161+
consec(num: number): boolean {
162+
this.cnt = this.val === num ? this.cnt + 1 : 0;
163+
return this.cnt >= this.k;
164+
}
165+
}
166+
167+
/**
168+
* Your DataStream object will be instantiated and called as such:
169+
* var obj = new DataStream(value, k)
170+
* var param_1 = obj.consec(num)
171+
*/
172+
```
173+
147174
### **...**
148175

149176
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class DataStream {
2+
private val: number;
3+
private k: number;
4+
private cnt: number;
5+
6+
constructor(value: number, k: number) {
7+
this.val = value;
8+
this.k = k;
9+
this.cnt = 0;
10+
}
11+
12+
consec(num: number): boolean {
13+
this.cnt = this.val === num ? this.cnt + 1 : 0;
14+
return this.cnt >= this.k;
15+
}
16+
}
17+
18+
/**
19+
* Your DataStream object will be instantiated and called as such:
20+
* var obj = new DataStream(value, k)
21+
* var param_1 = obj.consec(num)
22+
*/

‎solution/2500-2599/2527.Find Xor-Beauty of Array/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ func xorBeauty(nums []int) (ans int) {
125125
}
126126
```
127127

128+
### **TypeScript**
129+
130+
```ts
131+
function xorBeauty(nums: number[]): number {
132+
return nums.reduce((acc, cur) => acc ^ cur, 0);
133+
}
134+
```
135+
128136
### **...**
129137

130138
```

‎solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ func xorBeauty(nums []int) (ans int) {
105105
}
106106
```
107107

108+
### **TypeScript**
109+
110+
```ts
111+
function xorBeauty(nums: number[]): number {
112+
return nums.reduce((acc, cur) => acc ^ cur, 0);
113+
}
114+
```
115+
108116
### **...**
109117

110118
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function xorBeauty(nums: number[]): number {
2+
return nums.reduce((acc, cur) => acc ^ cur, 0);
3+
}

0 commit comments

Comments
 (0)
Please sign in to comment.