Skip to content

Commit 97fc76c

Browse files
authored
feat: add solutions to lc problems: No.3120~3123 (doocs#2637)
* No.3120.Count the Number of Special Characters I * No.3121.Count the Number of Special Characters II * No.3122.Minimum Number of Operations to Satisfy Conditions * No.3123.Find Edges in Shortest Paths
1 parent a62b6e2 commit 97fc76c

File tree

31 files changed

+1523
-39
lines changed

31 files changed

+1523
-39
lines changed

solution/0300-0399/0377.Combination Sum IV/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func combinationSum4(nums []int, target int) int {
131131

132132
```ts
133133
function combinationSum4(nums: number[], target: number): number {
134-
const f: number[] = new Array(target + 1).fill(0);
134+
const f: number[] = Array(target + 1).fill(0);
135135
f[0] = 1;
136136
for (let i = 1; i <= target; ++i) {
137137
for (const x of nums) {
@@ -151,7 +151,7 @@ function combinationSum4(nums: number[], target: number): number {
151151
* @return {number}
152152
*/
153153
var combinationSum4 = function (nums, target) {
154-
const f = new Array(target + 1).fill(0);
154+
const f = Array(target + 1).fill(0);
155155
f[0] = 1;
156156
for (let i = 1; i <= target; ++i) {
157157
for (const x of nums) {

solution/0300-0399/0377.Combination Sum IV/README_EN.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ Note that different sequences are counted as different combinations.
5050

5151
## Solutions
5252

53-
### Solution 1
53+
### Solution 1: Dynamic Programming
54+
55+
We define $f[i]$ as the number of combinations that sum up to $i$. Initially, $f[0] = 1$, and the rest $f[i] = 0$. The final answer is $f[target]$.
56+
57+
For $f[i]$, we can enumerate each element $x$ in the array. If $i \ge x$, then $f[i] = f[i] + f[i - x]$.
58+
59+
Finally, return $f[target]$.
60+
61+
The time complexity is $O(n \times target)$, and the space complexity is $O(target)$, where $n$ is the length of the array.
5462

5563
<!-- tabs:start -->
5664

@@ -118,7 +126,7 @@ func combinationSum4(nums []int, target int) int {
118126

119127
```ts
120128
function combinationSum4(nums: number[], target: number): number {
121-
const f: number[] = new Array(target + 1).fill(0);
129+
const f: number[] = Array(target + 1).fill(0);
122130
f[0] = 1;
123131
for (let i = 1; i <= target; ++i) {
124132
for (const x of nums) {
@@ -138,7 +146,7 @@ function combinationSum4(nums: number[], target: number): number {
138146
* @return {number}
139147
*/
140148
var combinationSum4 = function (nums, target) {
141-
const f = new Array(target + 1).fill(0);
149+
const f = Array(target + 1).fill(0);
142150
f[0] = 1;
143151
for (let i = 1; i <= target; ++i) {
144152
for (const x of nums) {

solution/0300-0399/0377.Combination Sum IV/Solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @return {number}
55
*/
66
var combinationSum4 = function (nums, target) {
7-
const f = new Array(target + 1).fill(0);
7+
const f = Array(target + 1).fill(0);
88
f[0] = 1;
99
for (let i = 1; i <= target; ++i) {
1010
for (const x of nums) {

solution/0300-0399/0377.Combination Sum IV/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function combinationSum4(nums: number[], target: number): number {
2-
const f: number[] = new Array(target + 1).fill(0);
2+
const f: number[] = Array(target + 1).fill(0);
33
f[0] = 1;
44
for (let i = 1; i <= target; ++i) {
55
for (const x of nums) {

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

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,87 @@
6161

6262
## 解法
6363

64-
### 方法一
64+
### 方法一:哈希表或数组
65+
66+
我们用一个哈希表或数组 $s$ 来记录字符串 $word$ 中出现的字符。然后遍历 $26$ 个字母,如果小写字母和大写字母都在 $s$ 中出现,则特殊字符的数量加一。
67+
68+
最后返回特殊字符的数量即可。
69+
70+
时间复杂度 $O(n + |\Sigma|)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 为字符串 $word$ 的长度;而 $|\Sigma|$ 为字符集大小,本题中 $|\Sigma| \leq 128$。
6571

6672
<!-- tabs:start -->
6773

6874
```python
69-
75+
class Solution:
76+
def numberOfSpecialChars(self, word: str) -> int:
77+
s = set(word)
78+
return sum(a in s and b in s for a, b in zip(ascii_lowercase, ascii_uppercase))
7079
```
7180

7281
```java
73-
82+
class Solution {
83+
public int numberOfSpecialChars(String word) {
84+
boolean[] s = new boolean['z' + 1];
85+
for (int i = 0; i < word.length(); ++i) {
86+
s[word.charAt(i)] = true;
87+
}
88+
int ans = 0;
89+
for (int i = 0; i < 26; ++i) {
90+
if (s['a' + i] && s['A' + i]) {
91+
++ans;
92+
}
93+
}
94+
return ans;
95+
}
96+
}
7497
```
7598

7699
```cpp
77-
100+
class Solution {
101+
public:
102+
int numberOfSpecialChars(string word) {
103+
vector<bool> s('z' + 1);
104+
for (char& c : word) {
105+
s[c] = true;
106+
}
107+
int ans = 0;
108+
for (int i = 0; i < 26; ++i) {
109+
ans += s['a' + i] && s['A' + i];
110+
}
111+
return ans;
112+
}
113+
};
78114
```
79115
80116
```go
117+
func numberOfSpecialChars(word string) (ans int) {
118+
s := make([]bool, 'z'+1)
119+
for _, c := range word {
120+
s[c] = true
121+
}
122+
for i := 0; i < 26; i++ {
123+
if s['a'+i] && s['A'+i] {
124+
ans++
125+
}
126+
}
127+
return
128+
}
129+
```
81130

131+
```ts
132+
function numberOfSpecialChars(word: string): number {
133+
const s: boolean[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => false);
134+
for (let i = 0; i < word.length; ++i) {
135+
s[word.charCodeAt(i)] = true;
136+
}
137+
let ans: number = 0;
138+
for (let i = 0; i < 26; ++i) {
139+
if (s['a'.charCodeAt(0) + i] && s['A'.charCodeAt(0) + i]) {
140+
++ans;
141+
}
142+
}
143+
return ans;
144+
}
82145
```
83146

84147
<!-- tabs:end -->

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

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,87 @@
5757

5858
## Solutions
5959

60-
### Solution 1
60+
### Solution 1: Hash Table or Array
61+
62+
We use a hash table or array $s$ to record the characters that appear in the string $word$. Then we traverse the 26 letters. If both the lowercase and uppercase letters appear in $s$, the count of special characters is incremented by one.
63+
64+
Finally, return the count of special characters.
65+
66+
The time complexity is $O(n + |\Sigma|)$, and the space complexity is $O(|\Sigma|)$. Where $n$ is the length of the string $word$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| \leq 128$.
6167

6268
<!-- tabs:start -->
6369

6470
```python
65-
71+
class Solution:
72+
def numberOfSpecialChars(self, word: str) -> int:
73+
s = set(word)
74+
return sum(a in s and b in s for a, b in zip(ascii_lowercase, ascii_uppercase))
6675
```
6776

6877
```java
69-
78+
class Solution {
79+
public int numberOfSpecialChars(String word) {
80+
boolean[] s = new boolean['z' + 1];
81+
for (int i = 0; i < word.length(); ++i) {
82+
s[word.charAt(i)] = true;
83+
}
84+
int ans = 0;
85+
for (int i = 0; i < 26; ++i) {
86+
if (s['a' + i] && s['A' + i]) {
87+
++ans;
88+
}
89+
}
90+
return ans;
91+
}
92+
}
7093
```
7194

7295
```cpp
73-
96+
class Solution {
97+
public:
98+
int numberOfSpecialChars(string word) {
99+
vector<bool> s('z' + 1);
100+
for (char& c : word) {
101+
s[c] = true;
102+
}
103+
int ans = 0;
104+
for (int i = 0; i < 26; ++i) {
105+
ans += s['a' + i] && s['A' + i];
106+
}
107+
return ans;
108+
}
109+
};
74110
```
75111
76112
```go
113+
func numberOfSpecialChars(word string) (ans int) {
114+
s := make([]bool, 'z'+1)
115+
for _, c := range word {
116+
s[c] = true
117+
}
118+
for i := 0; i < 26; i++ {
119+
if s['a'+i] && s['A'+i] {
120+
ans++
121+
}
122+
}
123+
return
124+
}
125+
```
77126

127+
```ts
128+
function numberOfSpecialChars(word: string): number {
129+
const s: boolean[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => false);
130+
for (let i = 0; i < word.length; ++i) {
131+
s[word.charCodeAt(i)] = true;
132+
}
133+
let ans: number = 0;
134+
for (let i = 0; i < 26; ++i) {
135+
if (s['a'.charCodeAt(0) + i] && s['A'.charCodeAt(0) + i]) {
136+
++ans;
137+
}
138+
}
139+
return ans;
140+
}
78141
```
79142

80143
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int numberOfSpecialChars(string word) {
4+
vector<bool> s('z' + 1);
5+
for (char& c : word) {
6+
s[c] = true;
7+
}
8+
int ans = 0;
9+
for (int i = 0; i < 26; ++i) {
10+
ans += s['a' + i] && s['A' + i];
11+
}
12+
return ans;
13+
}
14+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func numberOfSpecialChars(word string) (ans int) {
2+
s := make([]bool, 'z'+1)
3+
for _, c := range word {
4+
s[c] = true
5+
}
6+
for i := 0; i < 26; i++ {
7+
if s['a'+i] && s['A'+i] {
8+
ans++
9+
}
10+
}
11+
return
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int numberOfSpecialChars(String word) {
3+
boolean[] s = new boolean['z' + 1];
4+
for (int i = 0; i < word.length(); ++i) {
5+
s[word.charAt(i)] = true;
6+
}
7+
int ans = 0;
8+
for (int i = 0; i < 26; ++i) {
9+
if (s['a' + i] && s['A' + i]) {
10+
++ans;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def numberOfSpecialChars(self, word: str) -> int:
3+
s = set(word)
4+
return sum(a in s and b in s for a, b in zip(ascii_lowercase, ascii_uppercase))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy