Skip to content

Commit 7c610c0

Browse files
authored
feat: update solutions to lc problems: No.2262,2264 (#1719)
* No.2262.Total Appeal of A String * No.2264.Largest 3-Same-Digit Number in String
1 parent 9399197 commit 7c610c0

File tree

10 files changed

+155
-106
lines changed

10 files changed

+155
-106
lines changed

solution/2200-2299/2262.Total Appeal of A String/README.md

+20-12
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,20 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
遍历字符串 s 每个字符 `s[i]`, 维护以 `s[i]` 结尾的子串的引力值之和 t,遍历过程中累加 t 得到结果。
59+
**方法一:枚举**
6060

61-
若当前遍历到字符 `s[i]`,对应的引力值 t 的计算逻辑为:
61+
我们可以枚举以每个字符 $s[i]$ 结尾的字符串,计算其引力值之和 $t$,最后将所有 $t$ 相加即可。
6262

63-
1. 如果 `s[i]` 在之前没出现过,那么以 `s[i-1]` 结尾的每个子串的引力值都会加上 1,引力值之和会增加 i,再加上 1(`s[i]` 单独组成的子串的引力值),得到 `s[i]` 的引力值 t。
64-
1. 如果 `s[i]` 在之前出现过,定义最近一次出现的下标为 j,那么向子串 `s[0..i-1], s[1..i-1], ..., s[j..i-1]` 的末尾添加 `s[i]`,引力值不变。而 `s[j+1..i-1], s[j+2..i=1], ..., s[i-1..i-1]` 由于不包含 s[i],这些子串的引力值增加 1,因此有 i-j-1 个子串的引力值会增加 1,引力值之和增加 i-j-1,再加上 1,得到 `s[i]` 的引力值 t。
63+
考虑遍历到 $s[i]$ 时,即把 $s[i]$ 添加到以 $s[i-1]$ 结尾的子字符串的后面,其引力值之和 $t$ 的变化情况:
6564

66-
此过程中,我们用 pos 记录每个字符最近一次出现的位置。
65+
1. 如果 $s[i]$ 在之前没出现过,那么所有以 $s[i-1]$ 结尾的子字符串的引力值都会增加 $1$,共有 $i$ 个,所以 $t$ 增加 $i$,再加上 $s[i]$ 自身的引力值 $1$,所以 $t$ 一共增加 $i+1$;
66+
1. 如果 $s[i]$ 在之前出现过,不妨记上次出现的的位置为 $j$,那么我们向子字符串 $s[0..i-1]$, $[1..i-1]$, $s[2..i-1]$, $\cdots$, $s[j..i-1]$ 后面添加 $s[i]$,这些子字符串的引力值不会发生变化,因为 $s[i]$ 已经在这些子字符串中出现过了;而子字符串 $s[j+1..i-1]$, $s[j+2..i-1]$, $\cdots$, $s[i-1]$ 的引力值都会增加 $1$,共有 $i-j-1$ 个,所以 $t$ 增加 $i-j-1$,再加上 $s[i]$ 自身的引力值 $1$,所以 $t$ 一共增加 $i-j$。
67+
68+
综上,我们可以用一个数组 $pos$ 记录每个字符上次出现的位置,初始时所有位置都为 $-1$,
69+
70+
接下来,我们遍历字符串,每一次我们更新以当前字符结尾的子字符串的引力值之和 $t = t + i - pos[c]$,其中 $c$ 是当前字符,累加 $t$ 到答案中。然后我们更新 $pos[c]$ 为当前位置 $i$。继续遍历直到字符串结束。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$,其中 $n$ 是字符串 $s$ 的长度;而 $|\Sigma|$ 是字符集的大小,本题中 $|\Sigma| = 26$。
6773

6874
<!-- tabs:start -->
6975

@@ -148,15 +154,17 @@ func appealSum(s string) int64 {
148154

149155
```ts
150156
function appealSum(s: string): number {
157+
const pos: number[] = Array(26).fill(-1);
151158
const n = s.length;
152-
let dp = new Array(n + 1).fill(0);
153-
const hashMap = new Map();
154-
for (let i = 0; i < n; i++) {
155-
const c = s.charAt(i);
156-
dp[i + 1] = dp[i] + i + 1 - (hashMap.get(c) || 0);
157-
hashMap.set(c, i + 1);
159+
let ans = 0;
160+
let t = 0;
161+
for (let i = 0; i < n; ++i) {
162+
const c = s.charCodeAt(i) - 97;
163+
t += i - pos[c];
164+
ans += t;
165+
pos[c] = i;
158166
}
159-
return dp.reduce((a, c) => a + c, 0);
167+
return ans;
160168
}
161169
```
162170

solution/2200-2299/2262.Total Appeal of A String/README_EN.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ The total sum is 4 + 6 + 6 + 4 = 20.
5252

5353
## Solutions
5454

55+
**Solution 1: Enumeration**
56+
57+
We can enumerate all the substrings that end with each character $s[i]$ and calculate their gravitational value sum $t$. Finally, we add up all the $t$ to get the total gravitational value sum.
58+
59+
When we reach $s[i]$, which is added to the end of the substring that ends with $s[i-1]$, we consider the change of the gravitational value sum $t$:
60+
61+
If $s[i]$ has not appeared before, then the gravitational value of all substrings that end with $s[i-1]$ will increase by $1$, and there are a total of $i$ such substrings. Therefore, $t$ increases by $i$, plus the gravitational value of $s[i]$ itself, which is $1$. Therefore, $t$ increases by a total of $i+1$.
62+
63+
If $s[i]$ has appeared before, let the last appearance position be $j$. Then we add $s[i]$ to the end of the substrings $s[0..i-1]$, $[1..i-1]$, $s[2..i-1]$, $\cdots$, $s[j..i-1]$. The gravitational value of these substrings will not change because $s[i]$ has already appeared in these substrings. The gravitational value of the substrings $s[j+1..i-1]$, $s[j+2..i-1]$, $\cdots$, $s[i-1]$ will increase by $1$, and there are a total of $i-j-1$ such substrings. Therefore, $t$ increases by $i-j-1$, plus the gravitational value of $s[i]$ itself, which is $1$. Therefore, $t$ increases by a total of $i-j$.
64+
Therefore, we can use an array $pos$ to record the last appearance position of each character. Initially, all positions are set to $-1$.
65+
66+
Next, we traverse the string, and each time we update the gravitational value sum $t$ of the substring that ends with the current character to $t = t + i - pos[c]$, where $c$ is the current character. We add $t$ to the answer. Then we update $pos[c]$ to the current position $i$. We continue to traverse until the end of the string.
67+
68+
The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$, where $n$ is the length of the string $s$, and $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| = 26$.
69+
5570
<!-- tabs:start -->
5671

5772
### **Python3**
@@ -131,15 +146,17 @@ func appealSum(s string) int64 {
131146

132147
```ts
133148
function appealSum(s: string): number {
149+
const pos: number[] = Array(26).fill(-1);
134150
const n = s.length;
135-
let dp = new Array(n + 1).fill(0);
136-
const hashMap = new Map();
137-
for (let i = 0; i < n; i++) {
138-
const c = s.charAt(i);
139-
dp[i + 1] = dp[i] + i + 1 - (hashMap.get(c) || 0);
140-
hashMap.set(c, i + 1);
151+
let ans = 0;
152+
let t = 0;
153+
for (let i = 0; i < n; ++i) {
154+
const c = s.charCodeAt(i) - 97;
155+
t += i - pos[c];
156+
ans += t;
157+
pos[c] = i;
141158
}
142-
return dp.reduce((a, c) => a + c, 0);
159+
return ans;
143160
}
144161
```
145162

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
function appealSum(s: string): number {
2+
const pos: number[] = Array(26).fill(-1);
23
const n = s.length;
3-
let dp = new Array(n + 1).fill(0);
4-
const hashMap = new Map();
5-
for (let i = 0; i < n; i++) {
6-
const c = s.charAt(i);
7-
dp[i + 1] = dp[i] + i + 1 - (hashMap.get(c) || 0);
8-
hashMap.set(c, i + 1);
4+
let ans = 0;
5+
let t = 0;
6+
for (let i = 0; i < n; ++i) {
7+
const c = s.charCodeAt(i) - 97;
8+
t += i - pos[c];
9+
ans += t;
10+
pos[c] = i;
911
}
10-
return dp.reduce((a, c) => a + c, 0);
12+
return ans;
1113
}

solution/2200-2299/2264.Largest 3-Same-Digit Number in String/README.md

+34-24
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:枚举**
66+
67+
我们可以从大到小枚举每个数字 $i$,其中 $0 \le i \le 9$,然后判断连续的三个 $i$ 构成的字符串 $s$ 是否是 $num$ 的子串,若是,直接返回 $s$ 即可。
68+
69+
若枚举完所有的 $i$ 都没有找到满足条件的字符串,则返回空字符串。
70+
71+
时间复杂度 $O(10 \times n)$,其中 $n$ 是字符串 $num$ 的长度。空间复杂度 $O(1)$。
72+
6573
<!-- tabs:start -->
6674

6775
### **Python3**
@@ -72,10 +80,9 @@
7280
class Solution:
7381
def largestGoodInteger(self, num: str) -> str:
7482
for i in range(9, -1, -1):
75-
t = str(i) * 3
76-
if t in num:
77-
return t
78-
return ''
83+
if (s := str(i) * 3) in num:
84+
return s
85+
return ""
7986
```
8087

8188
### **Java**
@@ -86,37 +93,27 @@ class Solution:
8693
class Solution {
8794
public String largestGoodInteger(String num) {
8895
for (int i = 9; i >= 0; i--) {
89-
String ret = String.valueOf(i).repeat(3);
90-
if (num.contains(ret)) {
91-
return ret;
96+
String s = String.valueOf(i).repeat(3);
97+
if (num.contains(s)) {
98+
return s;
9299
}
93100
}
94101
return "";
95102
}
96103
}
97104
```
98105

99-
### **TypeScript**
100-
101-
```ts
102-
function largestGoodInteger(num: string): string {
103-
for (let i = 9; i >= 0; i--) {
104-
const c = String(i).repeat(3);
105-
if (num.includes(c)) return c;
106-
}
107-
return '';
108-
}
109-
```
110-
111106
### **C++**
112107

113108
```cpp
114109
class Solution {
115110
public:
116111
string largestGoodInteger(string num) {
117112
for (char i = '9'; i >= '0'; --i) {
118-
string t(3, i);
119-
if (num.find(t) != string::npos) return t;
113+
string s(3, i);
114+
if (num.find(s) != string::npos) {
115+
return s;
116+
}
120117
}
121118
return "";
122119
}
@@ -128,15 +125,28 @@ public:
128125
```go
129126
func largestGoodInteger(num string) string {
130127
for c := '9'; c >= '0'; c-- {
131-
t := strings.Repeat(string(c), 3)
132-
if strings.Contains(num, t) {
133-
return t
128+
if s := strings.Repeat(string(c), 3); strings.Contains(num, s) {
129+
return s
134130
}
135131
}
136132
return ""
137133
}
138134
```
139135

136+
### **TypeScript**
137+
138+
```ts
139+
function largestGoodInteger(num: string): string {
140+
for (let i = 9; i >= 0; i--) {
141+
const s = String(i).repeat(3);
142+
if (num.includes(s)) {
143+
return s;
144+
}
145+
}
146+
return '';
147+
}
148+
```
149+
140150
### **...**
141151

142152
```

solution/2200-2299/2264.Largest 3-Same-Digit Number in String/README_EN.md

+34-24
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656

5757
## Solutions
5858

59+
**Solution 1: Enumeration**
60+
61+
We can enumerate each digit $i$ from large to small, where $0 \le i \le 9$, and then check whether the string $s$ consisting of three consecutive $i$ is a substring of $num$. If it is, we directly return $s$.
62+
63+
If we have enumerated all the possible values of $i$ and still haven't found a substring that satisfies the condition, we return an empty string.
64+
65+
The time complexity is $O(10 \times n)$, where $n$ is the length of the string $num$. The space complexity is $O(1)$.
66+
5967
<!-- tabs:start -->
6068

6169
### **Python3**
@@ -64,10 +72,9 @@
6472
class Solution:
6573
def largestGoodInteger(self, num: str) -> str:
6674
for i in range(9, -1, -1):
67-
t = str(i) * 3
68-
if t in num:
69-
return t
70-
return ''
75+
if (s := str(i) * 3) in num:
76+
return s
77+
return ""
7178
```
7279

7380
### **Java**
@@ -76,37 +83,27 @@ class Solution:
7683
class Solution {
7784
public String largestGoodInteger(String num) {
7885
for (int i = 9; i >= 0; i--) {
79-
String ret = String.valueOf(i).repeat(3);
80-
if (num.contains(ret)) {
81-
return ret;
86+
String s = String.valueOf(i).repeat(3);
87+
if (num.contains(s)) {
88+
return s;
8289
}
8390
}
8491
return "";
8592
}
8693
}
8794
```
8895

89-
### **TypeScript**
90-
91-
```ts
92-
function largestGoodInteger(num: string): string {
93-
for (let i = 9; i >= 0; i--) {
94-
const c = String(i).repeat(3);
95-
if (num.includes(c)) return c;
96-
}
97-
return '';
98-
}
99-
```
100-
10196
### **C++**
10297

10398
```cpp
10499
class Solution {
105100
public:
106101
string largestGoodInteger(string num) {
107102
for (char i = '9'; i >= '0'; --i) {
108-
string t(3, i);
109-
if (num.find(t) != string::npos) return t;
103+
string s(3, i);
104+
if (num.find(s) != string::npos) {
105+
return s;
106+
}
110107
}
111108
return "";
112109
}
@@ -118,15 +115,28 @@ public:
118115
```go
119116
func largestGoodInteger(num string) string {
120117
for c := '9'; c >= '0'; c-- {
121-
t := strings.Repeat(string(c), 3)
122-
if strings.Contains(num, t) {
123-
return t
118+
if s := strings.Repeat(string(c), 3); strings.Contains(num, s) {
119+
return s
124120
}
125121
}
126122
return ""
127123
}
128124
```
129125

126+
### **TypeScript**
127+
128+
```ts
129+
function largestGoodInteger(num: string): string {
130+
for (let i = 9; i >= 0; i--) {
131+
const s = String(i).repeat(3);
132+
if (num.includes(s)) {
133+
return s;
134+
}
135+
}
136+
return '';
137+
}
138+
```
139+
130140
### **...**
131141

132142
```
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
class Solution {
2-
public:
3-
string largestGoodInteger(string num) {
4-
for (char i = '9'; i >= '0'; --i) {
5-
string t(3, i);
6-
if (num.find(t) != string::npos) return t;
7-
}
8-
return "";
9-
}
1+
class Solution {
2+
public:
3+
string largestGoodInteger(string num) {
4+
for (char i = '9'; i >= '0'; --i) {
5+
string s(3, i);
6+
if (num.find(s) != string::npos) {
7+
return s;
8+
}
9+
}
10+
return "";
11+
}
1012
};

solution/2200-2299/2264.Largest 3-Same-Digit Number in String/Solution.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
func largestGoodInteger(num string) string {
22
for c := '9'; c >= '0'; c-- {
3-
t := strings.Repeat(string(c), 3)
4-
if strings.Contains(num, t) {
5-
return t
3+
if s := strings.Repeat(string(c), 3); strings.Contains(num, s) {
4+
return s
65
}
76
}
87
return ""
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
class Solution {
2-
public String largestGoodInteger(String num) {
3-
for (int i = 9; i >= 0; i--) {
4-
String ret = String.valueOf(i).repeat(3);
5-
if (num.contains(ret)) {
6-
return ret;
7-
}
8-
}
9-
return "";
10-
}
11-
}
1+
class Solution {
2+
public String largestGoodInteger(String num) {
3+
for (int i = 9; i >= 0; i--) {
4+
String s = String.valueOf(i).repeat(3);
5+
if (num.contains(s)) {
6+
return s;
7+
}
8+
}
9+
return "";
10+
}
11+
}

0 commit comments

Comments
 (0)