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 problem: No.2083 #4008

Merged
merged 1 commit into from
Jan 31, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ tags:

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

我们可以用数组或哈希表统计字符串中每个字母出现的次数,然后遍历字符串,对于每个字母,其出现的次数即为以该字母开头和结尾的子串的个数,将所有字母的出现次数相加即为答案
我们可以用哈希表或者一个长度为 $26$ 的数组 $\textit{cnt}$ 来记录每个字符出现的次数

时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串的长度,而 $C$ 为字符集的大小。本题中 $C = 26$。
遍历字符串 $\textit{s}$,对于每个字符 $\textit{c}$,我们将 $\textit{cnt}[c]$ 的值加 $1$,然后将 $\textit{cnt}[c]$ 的值加到答案中。

最后返回答案即可。

时间复杂度 $O(n)$,其中 $n$ 是字符串 $\textit{s}$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,这里是小写英文字母,所以 $|\Sigma|=26$。

<!-- tabs:start -->

Expand All @@ -100,10 +104,8 @@ class Solution {
public long numberOfSubstrings(String s) {
int[] cnt = new int[26];
long ans = 0;
for (int i = 0; i < s.length(); ++i) {
int j = s.charAt(i) - 'a';
++cnt[j];
ans += cnt[j];
for (char c : s.toCharArray()) {
ans += ++cnt[c - 'a'];
}
return ans;
}
Expand Down Expand Up @@ -140,6 +142,55 @@ func numberOfSubstrings(s string) (ans int64) {
}
```

#### TypeScript

```ts
function numberOfSubstrings(s: string): number {
const cnt: Record<string, number> = {};
let ans = 0;
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
ans += cnt[c];
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn number_of_substrings(s: String) -> i64 {
let mut cnt = [0; 26];
let mut ans = 0_i64;
for c in s.chars() {
let idx = (c as u8 - b'a') as usize;
cnt[idx] += 1;
ans += cnt[idx];
}
ans
}
}
```

#### JavaScript

```js
/**
* @param {string} s
* @return {number}
*/
var numberOfSubstrings = function (s) {
const cnt = {};
let ans = 0;
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
ans += cnt[c];
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ The substring of length 1 that starts and ends with the same letter is: &quot;a&

<!-- solution:start -->

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

We can use a hash table or an array $\textit{cnt}$ of length $26$ to record the occurrences of each character.

Traverse the string $\textit{s}$. For each character $\textit{c}$, increment the value of $\textit{cnt}[c]$ by $1$, and then add the value of $\textit{cnt}[c]$ to the answer.

Finally, return the answer.

The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. Here, it is lowercase English letters, so $|\Sigma|=26$.

<!-- tabs:start -->

Expand All @@ -94,10 +102,8 @@ class Solution {
public long numberOfSubstrings(String s) {
int[] cnt = new int[26];
long ans = 0;
for (int i = 0; i < s.length(); ++i) {
int j = s.charAt(i) - 'a';
++cnt[j];
ans += cnt[j];
for (char c : s.toCharArray()) {
ans += ++cnt[c - 'a'];
}
return ans;
}
Expand Down Expand Up @@ -134,6 +140,55 @@ func numberOfSubstrings(s string) (ans int64) {
}
```

#### TypeScript

```ts
function numberOfSubstrings(s: string): number {
const cnt: Record<string, number> = {};
let ans = 0;
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
ans += cnt[c];
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn number_of_substrings(s: String) -> i64 {
let mut cnt = [0; 26];
let mut ans = 0_i64;
for c in s.chars() {
let idx = (c as u8 - b'a') as usize;
cnt[idx] += 1;
ans += cnt[idx];
}
ans
}
}
```

#### JavaScript

```js
/**
* @param {string} s
* @return {number}
*/
var numberOfSubstrings = function (s) {
const cnt = {};
let ans = 0;
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
ans += cnt[c];
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class Solution {
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ class Solution {
public long numberOfSubstrings(String s) {
int[] cnt = new int[26];
long ans = 0;
for (int i = 0; i < s.length(); ++i) {
int j = s.charAt(i) - 'a';
++cnt[j];
ans += cnt[j];
for (char c : s.toCharArray()) {
ans += ++cnt[c - 'a'];
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @param {string} s
* @return {number}
*/
var numberOfSubstrings = function (s) {
const cnt = {};
let ans = 0;
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
ans += cnt[c];
}
return ans;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
impl Solution {
pub fn number_of_substrings(s: String) -> i64 {
let mut cnt = [0; 26];
let mut ans = 0_i64;
for c in s.chars() {
let idx = (c as u8 - b'a') as usize;
cnt[idx] += 1;
ans += cnt[idx];
}
ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function numberOfSubstrings(s: string): number {
const cnt: Record<string, number> = {};
let ans = 0;
for (const c of s) {
cnt[c] = (cnt[c] || 0) + 1;
ans += cnt[c];
}
return ans;
}