Skip to content

Commit 7833ad6

Browse files
authored
feat: add solutions for lc No.3120,3121 (#5191)
1 parent c4984df commit 7833ad6

10 files changed

Lines changed: 161 additions & 27 deletions

File tree

solution/3100-3199/3120.Count the Number of Special Characters I/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ function numberOfSpecialChars(word: string): number {
169169
}
170170
```
171171

172+
#### Rust
173+
174+
```rust
175+
impl Solution {
176+
pub fn number_of_special_chars(word: String) -> i32 {
177+
let mut s = [false; 128];
178+
for ch in word.chars() {
179+
s[ch as u8 as usize] = true;
180+
}
181+
let mut ans = 0;
182+
for i in 0..26 {
183+
if s[(b'a' + i) as usize] && s[(b'A' + i) as usize] {
184+
ans += 1;
185+
}
186+
}
187+
ans
188+
}
189+
}
190+
```
191+
172192
<!-- tabs:end -->
173193

174194
<!-- solution:end -->

solution/3100-3199/3120.Count the Number of Special Characters I/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ function numberOfSpecialChars(word: string): number {
167167
}
168168
```
169169

170+
#### Rust
171+
172+
```rust
173+
impl Solution {
174+
pub fn number_of_special_chars(word: String) -> i32 {
175+
let mut s = [false; 128];
176+
for ch in word.chars() {
177+
s[ch as u8 as usize] = true;
178+
}
179+
let mut ans = 0;
180+
for i in 0..26 {
181+
if s[(b'a' + i) as usize] && s[(b'A' + i) as usize] {
182+
ans += 1;
183+
}
184+
}
185+
ans
186+
}
187+
}
188+
```
189+
170190
<!-- tabs:end -->
171191

172192
<!-- solution:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn number_of_special_chars(word: String) -> i32 {
3+
let mut s = [false; 128];
4+
for ch in word.chars() {
5+
s[ch as u8 as usize] = true;
6+
}
7+
let mut ans = 0;
8+
for i in 0..26 {
9+
if s[(b'a' + i) as usize] && s[(b'A' + i) as usize] {
10+
ans += 1;
11+
}
12+
}
13+
ans
14+
}
15+
}

solution/3100-3199/3121.Count the Number of Special Characters II/README.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ class Solution {
120120
}
121121
int ans = 0;
122122
for (int i = 0; i < 26; ++i) {
123-
if (last['a' + i] > 0 && first['A' + i] > 0 && last['a' + i] < first['A' + i]) {
123+
int a = 'a' + i;
124+
int b = 'A' + i;
125+
if (last[a] > 0 && last[a] < first[b]) {
124126
++ans;
125127
}
126128
}
@@ -146,7 +148,7 @@ public:
146148
}
147149
int ans = 0;
148150
for (int i = 0; i < 26; ++i) {
149-
if (last['a' + i] && first['A' + i] && last['a' + i] < first['A' + i]) {
151+
if (last['a' + i] && last['a' + i] < first['A' + i]) {
150152
++ans;
151153
}
152154
}
@@ -168,7 +170,7 @@ func numberOfSpecialChars(word string) (ans int) {
168170
last[c] = i + 1
169171
}
170172
for i := 0; i < 26; i++ {
171-
if last['a'+i] > 0 && first['A'+i] > 0 && last['a'+i] < first['A'+i] {
173+
if last['a'+i] > 0 && last['a'+i] < first['A'+i] {
172174
ans++
173175
}
174176
}
@@ -191,18 +193,44 @@ function numberOfSpecialChars(word: string): number {
191193
}
192194
let ans: number = 0;
193195
for (let i = 0; i < 26; ++i) {
194-
if (
195-
last['a'.charCodeAt(0) + i] &&
196-
first['A'.charCodeAt(0) + i] &&
197-
last['a'.charCodeAt(0) + i] < first['A'.charCodeAt(0) + i]
198-
) {
196+
const a = 'a'.charCodeAt(0) + i;
197+
const b = 'A'.charCodeAt(0) + i;
198+
if (last[a] && last[a] < first[b]) {
199199
++ans;
200200
}
201201
}
202202
return ans;
203203
}
204204
```
205205

206+
#### Rust
207+
208+
```rust
209+
impl Solution {
210+
pub fn number_of_special_chars(word: String) -> i32 {
211+
let mut first = [0; 128];
212+
let mut last = [0; 128];
213+
for (i, ch) in word.chars().enumerate() {
214+
let j = ch as u8 as usize;
215+
let pos = (i + 1) as i32;
216+
if first[j] == 0 {
217+
first[j] = pos;
218+
}
219+
last[j] = pos;
220+
}
221+
let mut ans = 0;
222+
for i in 0..26 {
223+
let a = (b'a' + i) as usize;
224+
let b = (b'A' + i) as usize;
225+
if last[a] > 0 && last[a] < first[b] {
226+
ans += 1;
227+
}
228+
}
229+
ans
230+
}
231+
}
232+
```
233+
206234
<!-- tabs:end -->
207235

208236
<!-- solution:end -->

solution/3100-3199/3121.Count the Number of Special Characters II/README_EN.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ class Solution {
118118
}
119119
int ans = 0;
120120
for (int i = 0; i < 26; ++i) {
121-
if (last['a' + i] > 0 && first['A' + i] > 0 && last['a' + i] < first['A' + i]) {
121+
int a = 'a' + i;
122+
int b = 'A' + i;
123+
if (last[a] > 0 && last[a] < first[b]) {
122124
++ans;
123125
}
124126
}
@@ -144,7 +146,7 @@ public:
144146
}
145147
int ans = 0;
146148
for (int i = 0; i < 26; ++i) {
147-
if (last['a' + i] && first['A' + i] && last['a' + i] < first['A' + i]) {
149+
if (last['a' + i] && last['a' + i] < first['A' + i]) {
148150
++ans;
149151
}
150152
}
@@ -166,7 +168,7 @@ func numberOfSpecialChars(word string) (ans int) {
166168
last[c] = i + 1
167169
}
168170
for i := 0; i < 26; i++ {
169-
if last['a'+i] > 0 && first['A'+i] > 0 && last['a'+i] < first['A'+i] {
171+
if last['a'+i] > 0 && last['a'+i] < first['A'+i] {
170172
ans++
171173
}
172174
}
@@ -189,18 +191,44 @@ function numberOfSpecialChars(word: string): number {
189191
}
190192
let ans: number = 0;
191193
for (let i = 0; i < 26; ++i) {
192-
if (
193-
last['a'.charCodeAt(0) + i] &&
194-
first['A'.charCodeAt(0) + i] &&
195-
last['a'.charCodeAt(0) + i] < first['A'.charCodeAt(0) + i]
196-
) {
194+
const a = 'a'.charCodeAt(0) + i;
195+
const b = 'A'.charCodeAt(0) + i;
196+
if (last[a] && last[a] < first[b]) {
197197
++ans;
198198
}
199199
}
200200
return ans;
201201
}
202202
```
203203

204+
#### Rust
205+
206+
```rust
207+
impl Solution {
208+
pub fn number_of_special_chars(word: String) -> i32 {
209+
let mut first = [0; 128];
210+
let mut last = [0; 128];
211+
for (i, ch) in word.chars().enumerate() {
212+
let j = ch as u8 as usize;
213+
let pos = (i + 1) as i32;
214+
if first[j] == 0 {
215+
first[j] = pos;
216+
}
217+
last[j] = pos;
218+
}
219+
let mut ans = 0;
220+
for i in 0..26 {
221+
let a = (b'a' + i) as usize;
222+
let b = (b'A' + i) as usize;
223+
if last[a] > 0 && last[a] < first[b] {
224+
ans += 1;
225+
}
226+
}
227+
ans
228+
}
229+
}
230+
```
231+
204232
<!-- tabs:end -->
205233

206234
<!-- solution:end -->

solution/3100-3199/3121.Count the Number of Special Characters II/Solution.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ class Solution {
1212
}
1313
int ans = 0;
1414
for (int i = 0; i < 26; ++i) {
15-
if (last['a' + i] && first['A' + i] && last['a' + i] < first['A' + i]) {
15+
if (last['a' + i] && last['a' + i] < first['A' + i]) {
1616
++ans;
1717
}
1818
}
1919
return ans;
2020
}
21-
};
21+
};

solution/3100-3199/3121.Count the Number of Special Characters II/Solution.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ func numberOfSpecialChars(word string) (ans int) {
88
last[c] = i + 1
99
}
1010
for i := 0; i < 26; i++ {
11-
if last['a'+i] > 0 && first['A'+i] > 0 && last['a'+i] < first['A'+i] {
11+
if last['a'+i] > 0 && last['a'+i] < first['A'+i] {
1212
ans++
1313
}
1414
}
1515
return
16-
}
16+
}

solution/3100-3199/3121.Count the Number of Special Characters II/Solution.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ public int numberOfSpecialChars(String word) {
1111
}
1212
int ans = 0;
1313
for (int i = 0; i < 26; ++i) {
14-
if (last['a' + i] > 0 && first['A' + i] > 0 && last['a' + i] < first['A' + i]) {
14+
int a = 'a' + i;
15+
int b = 'A' + i;
16+
if (last[a] > 0 && last[a] < first[b]) {
1517
++ans;
1618
}
1719
}
1820
return ans;
1921
}
20-
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn number_of_special_chars(word: String) -> i32 {
3+
let mut first = [0; 128];
4+
let mut last = [0; 128];
5+
for (i, ch) in word.chars().enumerate() {
6+
let j = ch as u8 as usize;
7+
let pos = (i + 1) as i32;
8+
if first[j] == 0 {
9+
first[j] = pos;
10+
}
11+
last[j] = pos;
12+
}
13+
let mut ans = 0;
14+
for i in 0..26 {
15+
let a = (b'a' + i) as usize;
16+
let b = (b'A' + i) as usize;
17+
if last[a] > 0 && last[a] < first[b] {
18+
ans += 1;
19+
}
20+
}
21+
ans
22+
}
23+
}

solution/3100-3199/3121.Count the Number of Special Characters II/Solution.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ function numberOfSpecialChars(word: string): number {
1010
}
1111
let ans: number = 0;
1212
for (let i = 0; i < 26; ++i) {
13-
if (
14-
last['a'.charCodeAt(0) + i] &&
15-
first['A'.charCodeAt(0) + i] &&
16-
last['a'.charCodeAt(0) + i] < first['A'.charCodeAt(0) + i]
17-
) {
13+
const a = 'a'.charCodeAt(0) + i;
14+
const b = 'A'.charCodeAt(0) + i;
15+
if (last[a] && last[a] < first[b]) {
1816
++ans;
1917
}
2018
}

0 commit comments

Comments
 (0)