You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2200-2299/2262.Total Appeal of A String/README_EN.md
+24-7
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,21 @@ The total sum is 4 + 6 + 6 + 4 = 20.
52
52
53
53
## Solutions
54
54
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$.
Copy file name to clipboardexpand all lines: solution/2200-2299/2264.Largest 3-Same-Digit Number in String/README_EN.md
+34-24
Original file line number
Diff line number
Diff line change
@@ -56,6 +56,14 @@
56
56
57
57
## Solutions
58
58
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
+
59
67
<!-- tabs:start -->
60
68
61
69
### **Python3**
@@ -64,10 +72,9 @@
64
72
classSolution:
65
73
deflargestGoodInteger(self, num: str) -> str:
66
74
for i inrange(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""
71
78
```
72
79
73
80
### **Java**
@@ -76,37 +83,27 @@ class Solution:
76
83
classSolution {
77
84
publicStringlargestGoodInteger(Stringnum) {
78
85
for (int i =9; i >=0; i--) {
79
-
Stringret=String.valueOf(i).repeat(3);
80
-
if (num.contains(ret)) {
81
-
returnret;
86
+
Strings=String.valueOf(i).repeat(3);
87
+
if (num.contains(s)) {
88
+
returns;
82
89
}
83
90
}
84
91
return"";
85
92
}
86
93
}
87
94
```
88
95
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)) returnc;
96
-
}
97
-
return'';
98
-
}
99
-
```
100
-
101
96
### **C++**
102
97
103
98
```cpp
104
99
classSolution {
105
100
public:
106
101
string largestGoodInteger(string num) {
107
102
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
+
}
110
107
}
111
108
return "";
112
109
}
@@ -118,15 +115,28 @@ public:
118
115
```go
119
116
func largestGoodInteger(num string) string {
120
117
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) {
0 commit comments