diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README.md b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README.md index cd4c72cfa80ce..5ef8d3d8268fa 100644 --- a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README.md +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README.md @@ -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$。 @@ -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; } @@ -140,6 +142,55 @@ func numberOfSubstrings(s string) (ans int64) { } ``` +#### TypeScript + +```ts +function numberOfSubstrings(s: string): number { + const cnt: Record = {}; + 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; +}; +``` + diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README_EN.md b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README_EN.md index 0c829e8d2696e..6337008920090 100644 --- a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README_EN.md +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/README_EN.md @@ -70,7 +70,15 @@ The substring of length 1 that starts and ends with the same letter is: "a& -### 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$. @@ -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; } @@ -134,6 +140,55 @@ func numberOfSubstrings(s string) (ans int64) { } ``` +#### TypeScript + +```ts +function numberOfSubstrings(s: string): number { + const cnt: Record = {}; + 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; +}; +``` + diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp index 82e379e9e3d80..321451f092def 100644 --- a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp @@ -8,4 +8,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.java b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.java index 0fbb0fe463a67..561b4bf021ded 100644 --- a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.java +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.java @@ -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; } -} \ No newline at end of file +} diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.js b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.js new file mode 100644 index 0000000000000..3356fefc2bd04 --- /dev/null +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.js @@ -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; +}; diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.rs b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.rs new file mode 100644 index 0000000000000..c4610984491b4 --- /dev/null +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.rs @@ -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 + } +} diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.ts b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.ts new file mode 100644 index 0000000000000..33be7158409ae --- /dev/null +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.ts @@ -0,0 +1,9 @@ +function numberOfSubstrings(s: string): number { + const cnt: Record = {}; + let ans = 0; + for (const c of s) { + cnt[c] = (cnt[c] || 0) + 1; + ans += cnt[c]; + } + return ans; +}