Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cc4815a

Browse files
feat: add solutions to lc problem: No.3412 (doocs#3930)
No.3412.Find Mirror Score of a String
1 parent c644d65 commit cc4815a

File tree

7 files changed

+319
-8
lines changed

7 files changed

+319
-8
lines changed

‎solution/3400-3499/3412.Find Mirror Score of a String/README.md‎

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3412.Fi
7777

7878
<!-- solution:start -->
7979

80-
### 方法一
80+
### 方法一:哈希表
81+
82+
我们可以用一个哈希表 $\textit{d}$ 来存储每个未标记的字符的下标列表,其中键是字符,值是下标列表。
83+
84+
我们遍历字符串 $\textit{s},ドル对于每个字符 $\textit{x},ドル我们找到其镜像字符 $\textit{y},ドル如果 $\textit{d}$ 中存在 $\textit{y},ドル我们就取出 $\textit{y}$ 对应的下标列表 $\textit{ls},ドル取出 $\textit{ls}$ 的最后一个元素 $\textit{j},ドル并将 $\textit{j}$ 从 $\textit{ls}$ 中移除。如果 $\textit{ls}$ 变为空,我们就将 $\textit{y}$ 从 $\textit{d}$ 中移除。此时,我们就找到了一个满足条件的下标对 $(\textit{j}, \textit{i}),ドル并将 $\textit{i} - \textit{j}$ 加到答案中。否则,我们将 $\textit{x}$ 加入到 $\textit{d}$ 中。
85+
86+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 为字符串 $\textit{s}$ 的长度。
8187

8288
<!-- tabs:start -->
8389

8490
#### Python3
8591

8692
```python
87-
93+
class Solution:
94+
def calculateScore(self, s: str) -> int:
95+
d = defaultdict(list)
96+
ans = 0
97+
for i, x in enumerate(s):
98+
y = chr(ord("a") + ord("z") - ord(x))
99+
if d[y]:
100+
j = d[y].pop()
101+
ans += i - j
102+
else:
103+
d[x].append(i)
104+
return ans
88105
```
89106

90107
#### Java
91108

92109
```java
93-
110+
class Solution {
111+
public long calculateScore(String s) {
112+
Map<Character, List<Integer>> d = new HashMap<>(26);
113+
int n = s.length();
114+
long ans = 0;
115+
for (int i = 0; i < n; ++i) {
116+
char x = s.charAt(i);
117+
char y = (char) ('a' + 'z' - x);
118+
if (d.containsKey(y)) {
119+
var ls = d.get(y);
120+
int j = ls.remove(ls.size() - 1);
121+
if (ls.isEmpty()) {
122+
d.remove(y);
123+
}
124+
ans += i - j;
125+
} else {
126+
d.computeIfAbsent(x, k -> new ArrayList<>()).add(i);
127+
}
128+
}
129+
return ans;
130+
}
131+
}
94132
```
95133

96134
#### C++
97135

98136
```cpp
99-
137+
class Solution {
138+
public:
139+
long long calculateScore(string s) {
140+
unordered_map<char, vector<int>> d;
141+
int n = s.length();
142+
long long ans = 0;
143+
for (int i = 0; i < n; ++i) {
144+
char x = s[i];
145+
char y = 'a' + 'z' - x;
146+
if (d.contains(y)) {
147+
vector<int>& ls = d[y];
148+
int j = ls.back();
149+
ls.pop_back();
150+
if (ls.empty()) {
151+
d.erase(y);
152+
}
153+
ans += i - j;
154+
} else {
155+
d[x].push_back(i);
156+
}
157+
}
158+
return ans;
159+
}
160+
};
100161
```
101162
102163
#### Go
103164
104165
```go
166+
func calculateScore(s string) (ans int64) {
167+
d := make(map[rune][]int)
168+
for i, x := range s {
169+
y := 'a' + 'z' - x
170+
if ls, ok := d[y]; ok {
171+
j := ls[len(ls)-1]
172+
d[y] = ls[:len(ls)-1]
173+
if len(d[y]) == 0 {
174+
delete(d, y)
175+
}
176+
ans += int64(i - j)
177+
} else {
178+
d[x] = append(d[x], i)
179+
}
180+
}
181+
return
182+
}
183+
```
105184

185+
#### TypeScript
186+
187+
```ts
188+
function calculateScore(s: string): number {
189+
const d: Map<string, number[]> = new Map();
190+
const n = s.length;
191+
let ans = 0;
192+
for (let i = 0; i < n; i++) {
193+
const x = s[i];
194+
const y = String.fromCharCode('a'.charCodeAt(0) + 'z'.charCodeAt(0) - x.charCodeAt(0));
195+
196+
if (d.has(y)) {
197+
const ls = d.get(y)!;
198+
const j = ls.pop()!;
199+
if (ls.length === 0) {
200+
d.delete(y);
201+
}
202+
ans += i - j;
203+
} else {
204+
if (!d.has(x)) {
205+
d.set(x, []);
206+
}
207+
d.get(x)!.push(i);
208+
}
209+
}
210+
return ans;
211+
}
106212
```
107213

108214
<!-- tabs:end -->

‎solution/3400-3499/3412.Find Mirror Score of a String/README_EN.md‎

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3412.Fi
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Hash Table
79+
80+
We can use a hash table $\textit{d}$ to store the index list of each unmarked character, where the key is the character and the value is the list of indices.
81+
82+
We traverse the string $\textit{s},ドル and for each character $\textit{x},ドル we find its mirror character $\textit{y}$. If $\textit{d}$ contains $\textit{y},ドル we take out the index list $\textit{ls}$ corresponding to $\textit{y},ドル take out the last element $\textit{j}$ from $\textit{ls},ドル and remove $\textit{j}$ from $\textit{ls}$. If $\textit{ls}$ becomes empty, we remove $\textit{y}$ from $\textit{d}$. At this point, we have found a pair of indices $(\textit{j}, \textit{i})$ that meet the condition, and we add $\textit{i} - \textit{j}$ to the answer. Otherwise, we add $\textit{x}$ to $\textit{d}$.
83+
84+
The time complexity is $O(n),ドル and the space complexity is $O(n)$. Here, $n$ is the length of the string $\textit{s}$.
7985

8086
<!-- tabs:start -->
8187

8288
#### Python3
8389

8490
```python
85-
91+
class Solution:
92+
def calculateScore(self, s: str) -> int:
93+
d = defaultdict(list)
94+
ans = 0
95+
for i, x in enumerate(s):
96+
y = chr(ord("a") + ord("z") - ord(x))
97+
if d[y]:
98+
j = d[y].pop()
99+
ans += i - j
100+
else:
101+
d[x].append(i)
102+
return ans
86103
```
87104

88105
#### Java
89106

90107
```java
91-
108+
class Solution {
109+
public long calculateScore(String s) {
110+
Map<Character, List<Integer>> d = new HashMap<>(26);
111+
int n = s.length();
112+
long ans = 0;
113+
for (int i = 0; i < n; ++i) {
114+
char x = s.charAt(i);
115+
char y = (char) ('a' + 'z' - x);
116+
if (d.containsKey(y)) {
117+
var ls = d.get(y);
118+
int j = ls.remove(ls.size() - 1);
119+
if (ls.isEmpty()) {
120+
d.remove(y);
121+
}
122+
ans += i - j;
123+
} else {
124+
d.computeIfAbsent(x, k -> new ArrayList<>()).add(i);
125+
}
126+
}
127+
return ans;
128+
}
129+
}
92130
```
93131

94132
#### C++
95133

96134
```cpp
97-
135+
class Solution {
136+
public:
137+
long long calculateScore(string s) {
138+
unordered_map<char, vector<int>> d;
139+
int n = s.length();
140+
long long ans = 0;
141+
for (int i = 0; i < n; ++i) {
142+
char x = s[i];
143+
char y = 'a' + 'z' - x;
144+
if (d.contains(y)) {
145+
vector<int>& ls = d[y];
146+
int j = ls.back();
147+
ls.pop_back();
148+
if (ls.empty()) {
149+
d.erase(y);
150+
}
151+
ans += i - j;
152+
} else {
153+
d[x].push_back(i);
154+
}
155+
}
156+
return ans;
157+
}
158+
};
98159
```
99160
100161
#### Go
101162
102163
```go
164+
func calculateScore(s string) (ans int64) {
165+
d := make(map[rune][]int)
166+
for i, x := range s {
167+
y := 'a' + 'z' - x
168+
if ls, ok := d[y]; ok {
169+
j := ls[len(ls)-1]
170+
d[y] = ls[:len(ls)-1]
171+
if len(d[y]) == 0 {
172+
delete(d, y)
173+
}
174+
ans += int64(i - j)
175+
} else {
176+
d[x] = append(d[x], i)
177+
}
178+
}
179+
return
180+
}
181+
```
103182

183+
#### TypeScript
184+
185+
```ts
186+
function calculateScore(s: string): number {
187+
const d: Map<string, number[]> = new Map();
188+
const n = s.length;
189+
let ans = 0;
190+
for (let i = 0; i < n; i++) {
191+
const x = s[i];
192+
const y = String.fromCharCode('a'.charCodeAt(0) + 'z'.charCodeAt(0) - x.charCodeAt(0));
193+
194+
if (d.has(y)) {
195+
const ls = d.get(y)!;
196+
const j = ls.pop()!;
197+
if (ls.length === 0) {
198+
d.delete(y);
199+
}
200+
ans += i - j;
201+
} else {
202+
if (!d.has(x)) {
203+
d.set(x, []);
204+
}
205+
d.get(x)!.push(i);
206+
}
207+
}
208+
return ans;
209+
}
104210
```
105211

106212
<!-- tabs:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
long long calculateScore(string s) {
4+
unordered_map<char, vector<int>> d;
5+
int n = s.length();
6+
long long ans = 0;
7+
for (int i = 0; i < n; ++i) {
8+
char x = s[i];
9+
char y = 'a' + 'z' - x;
10+
if (d.contains(y)) {
11+
vector<int>& ls = d[y];
12+
int j = ls.back();
13+
ls.pop_back();
14+
if (ls.empty()) {
15+
d.erase(y);
16+
}
17+
ans += i - j;
18+
} else {
19+
d[x].push_back(i);
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func calculateScore(s string) (ans int64) {
2+
d := make(map[rune][]int)
3+
for i, x := range s {
4+
y := 'a' + 'z' - x
5+
if ls, ok := d[y]; ok {
6+
j := ls[len(ls)-1]
7+
d[y] = ls[:len(ls)-1]
8+
if len(d[y]) == 0 {
9+
delete(d, y)
10+
}
11+
ans += int64(i - j)
12+
} else {
13+
d[x] = append(d[x], i)
14+
}
15+
}
16+
return
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public long calculateScore(String s) {
3+
Map<Character, List<Integer>> d = new HashMap<>(26);
4+
int n = s.length();
5+
long ans = 0;
6+
for (int i = 0; i < n; ++i) {
7+
char x = s.charAt(i);
8+
char y = (char) ('a' + 'z' - x);
9+
if (d.containsKey(y)) {
10+
var ls = d.get(y);
11+
int j = ls.remove(ls.size() - 1);
12+
if (ls.isEmpty()) {
13+
d.remove(y);
14+
}
15+
ans += i - j;
16+
} else {
17+
d.computeIfAbsent(x, k -> new ArrayList<>()).add(i);
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def calculateScore(self, s: str) -> int:
3+
d = defaultdict(list)
4+
ans = 0
5+
for i, x in enumerate(s):
6+
y = chr(ord("a") + ord("z") - ord(x))
7+
if d[y]:
8+
j = d[y].pop()
9+
ans += i - j
10+
else:
11+
d[x].append(i)
12+
return ans
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function calculateScore(s: string): number {
2+
const d: Map<string, number[]> = new Map();
3+
const n = s.length;
4+
let ans = 0;
5+
for (let i = 0; i < n; i++) {
6+
const x = s[i];
7+
const y = String.fromCharCode('a'.charCodeAt(0) + 'z'.charCodeAt(0) - x.charCodeAt(0));
8+
9+
if (d.has(y)) {
10+
const ls = d.get(y)!;
11+
const j = ls.pop()!;
12+
if (ls.length === 0) {
13+
d.delete(y);
14+
}
15+
ans += i - j;
16+
} else {
17+
if (!d.has(x)) {
18+
d.set(x, []);
19+
}
20+
d.get(x)!.push(i);
21+
}
22+
}
23+
return ans;
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /