Skip to content

feat: add solutions to lc problems: No.3120~3123 #2637

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 1 commit into from
Apr 22, 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
4 changes: 2 additions & 2 deletions solution/0300-0399/0377.Combination Sum IV/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func combinationSum4(nums []int, target int) int {

```ts
function combinationSum4(nums: number[], target: number): number {
const f: number[] = new Array(target + 1).fill(0);
const f: number[] = Array(target + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= target; ++i) {
for (const x of nums) {
Expand All @@ -151,7 +151,7 @@ function combinationSum4(nums: number[], target: number): number {
* @return {number}
*/
var combinationSum4 = function (nums, target) {
const f = new Array(target + 1).fill(0);
const f = Array(target + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= target; ++i) {
for (const x of nums) {
Expand Down
14 changes: 11 additions & 3 deletions solution/0300-0399/0377.Combination Sum IV/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ Note that different sequences are counted as different combinations.

## Solutions

### Solution 1
### Solution 1: Dynamic Programming

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

For $f[i]$, we can enumerate each element $x$ in the array. If $i \ge x$, then $f[i] = f[i] + f[i - x]$.

Finally, return $f[target]$.

The time complexity is $O(n \times target)$, and the space complexity is $O(target)$, where $n$ is the length of the array.

<!-- tabs:start -->

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

```ts
function combinationSum4(nums: number[], target: number): number {
const f: number[] = new Array(target + 1).fill(0);
const f: number[] = Array(target + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= target; ++i) {
for (const x of nums) {
Expand All @@ -138,7 +146,7 @@ function combinationSum4(nums: number[], target: number): number {
* @return {number}
*/
var combinationSum4 = function (nums, target) {
const f = new Array(target + 1).fill(0);
const f = Array(target + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= target; ++i) {
for (const x of nums) {
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0377.Combination Sum IV/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @return {number}
*/
var combinationSum4 = function (nums, target) {
const f = new Array(target + 1).fill(0);
const f = Array(target + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= target; ++i) {
for (const x of nums) {
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0377.Combination Sum IV/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function combinationSum4(nums: number[], target: number): number {
const f: number[] = new Array(target + 1).fill(0);
const f: number[] = Array(target + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= target; ++i) {
for (const x of nums) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,87 @@

## 解法

### 方法一
### 方法一:哈希表或数组

我们用一个哈希表或数组 $s$ 来记录字符串 $word$ 中出现的字符。然后遍历 $26$ 个字母,如果小写字母和大写字母都在 $s$ 中出现,则特殊字符的数量加一。

最后返回特殊字符的数量即可。

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

<!-- tabs:start -->

```python

class Solution:
def numberOfSpecialChars(self, word: str) -> int:
s = set(word)
return sum(a in s and b in s for a, b in zip(ascii_lowercase, ascii_uppercase))
```

```java

class Solution {
public int numberOfSpecialChars(String word) {
boolean[] s = new boolean['z' + 1];
for (int i = 0; i < word.length(); ++i) {
s[word.charAt(i)] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (s['a' + i] && s['A' + i]) {
++ans;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int numberOfSpecialChars(string word) {
vector<bool> s('z' + 1);
for (char& c : word) {
s[c] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
ans += s['a' + i] && s['A' + i];
}
return ans;
}
};
```

```go
func numberOfSpecialChars(word string) (ans int) {
s := make([]bool, 'z'+1)
for _, c := range word {
s[c] = true
}
for i := 0; i < 26; i++ {
if s['a'+i] && s['A'+i] {
ans++
}
}
return
}
```

```ts
function numberOfSpecialChars(word: string): number {
const s: boolean[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => false);
for (let i = 0; i < word.length; ++i) {
s[word.charCodeAt(i)] = true;
}
let ans: number = 0;
for (let i = 0; i < 26; ++i) {
if (s['a'.charCodeAt(0) + i] && s['A'.charCodeAt(0) + i]) {
++ans;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,87 @@

## Solutions

### Solution 1
### Solution 1: Hash Table or Array

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.

Finally, return the count of special characters.

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

<!-- tabs:start -->

```python

class Solution:
def numberOfSpecialChars(self, word: str) -> int:
s = set(word)
return sum(a in s and b in s for a, b in zip(ascii_lowercase, ascii_uppercase))
```

```java

class Solution {
public int numberOfSpecialChars(String word) {
boolean[] s = new boolean['z' + 1];
for (int i = 0; i < word.length(); ++i) {
s[word.charAt(i)] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (s['a' + i] && s['A' + i]) {
++ans;
}
}
return ans;
}
}
```

```cpp

class Solution {
public:
int numberOfSpecialChars(string word) {
vector<bool> s('z' + 1);
for (char& c : word) {
s[c] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
ans += s['a' + i] && s['A' + i];
}
return ans;
}
};
```

```go
func numberOfSpecialChars(word string) (ans int) {
s := make([]bool, 'z'+1)
for _, c := range word {
s[c] = true
}
for i := 0; i < 26; i++ {
if s['a'+i] && s['A'+i] {
ans++
}
}
return
}
```

```ts
function numberOfSpecialChars(word: string): number {
const s: boolean[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => false);
for (let i = 0; i < word.length; ++i) {
s[word.charCodeAt(i)] = true;
}
let ans: number = 0;
for (let i = 0; i < 26; ++i) {
if (s['a'.charCodeAt(0) + i] && s['A'.charCodeAt(0) + i]) {
++ans;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int numberOfSpecialChars(string word) {
vector<bool> s('z' + 1);
for (char& c : word) {
s[c] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
ans += s['a' + i] && s['A' + i];
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func numberOfSpecialChars(word string) (ans int) {
s := make([]bool, 'z'+1)
for _, c := range word {
s[c] = true
}
for i := 0; i < 26; i++ {
if s['a'+i] && s['A'+i] {
ans++
}
}
return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public int numberOfSpecialChars(String word) {
boolean[] s = new boolean['z' + 1];
for (int i = 0; i < word.length(); ++i) {
s[word.charAt(i)] = true;
}
int ans = 0;
for (int i = 0; i < 26; ++i) {
if (s['a' + i] && s['A' + i]) {
++ans;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def numberOfSpecialChars(self, word: str) -> int:
s = set(word)
return sum(a in s and b in s for a, b in zip(ascii_lowercase, ascii_uppercase))
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function numberOfSpecialChars(word: string): number {
const s: boolean[] = Array.from({ length: 'z'.charCodeAt(0) + 1 }, () => false);
for (let i = 0; i < word.length; ++i) {
s[word.charCodeAt(i)] = true;
}
let ans: number = 0;
for (let i = 0; i < 26; ++i) {
if (s['a'.charCodeAt(0) + i] && s['A'.charCodeAt(0) + i]) {
++ans;
}
}
return ans;
}
Loading