Skip to content

Commit 5a698f5

Browse files
authored
feat: add solutions to lc problem: No.3481 (#4150)
No.3481.Apply Substitutions
1 parent e829715 commit 5a698f5

File tree

7 files changed

+357
-8
lines changed

7 files changed

+357
-8
lines changed

solution/3400-3499/3481.Apply Substitutions/README.md

+125-4
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,153 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3481.Ap
7676

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

79-
### 方法一
79+
### 方法一:哈希表 + 递归
80+
81+
我们用一个哈希表 $\textit{d}$ 来存储替换的映射关系,然后定义一个函数 $\textit{dfs}$ 来递归地替换字符串中的占位符。
82+
83+
函数 $\textit{dfs}$ 的执行逻辑如下:
84+
85+
1. 在字符串 $\textit{s}$ 中查找第一个占位符的起始位置 $i$,如果找不到,则返回 $\textit{s}$;
86+
2. 在字符串 $\textit{s}$ 中查找第一个占位符的结束位置 $j$,如果找不到,则返回 $\textit{s}$;
87+
3. 截取占位符的键值 $key$,然后递归地替换占位符的值 $d[key]$;
88+
4. 返回替换后的字符串。
89+
90+
在主函数中,我们调用 $\textit{dfs}$ 函数,传入文本字符串 $\textit{text}$,并返回结果。
91+
92+
时间复杂度 $O(m + n \times L)$,空间复杂度 $O(m + n \times L)$。其中 $m$ 为替换映射的长度,而 $n$ 和 $L$ 分别为文本字符串的长度和占位符的平均长度。
8093

8194
<!-- tabs:start -->
8295

8396
#### Python3
8497

8598
```python
86-
99+
class Solution:
100+
def applySubstitutions(self, replacements: List[List[str]], text: str) -> str:
101+
def dfs(s: str) -> str:
102+
i = s.find("%")
103+
if i == -1:
104+
return s
105+
j = s.find("%", i + 1)
106+
if j == -1:
107+
return s
108+
key = s[i + 1 : j]
109+
replacement = dfs(d[key])
110+
return s[:i] + replacement + dfs(s[j + 1 :])
111+
112+
d = {s: t for s, t in replacements}
113+
return dfs(text)
87114
```
88115

89116
#### Java
90117

91118
```java
92-
119+
class Solution {
120+
private final Map<String, String> d = new HashMap<>();
121+
122+
public String applySubstitutions(List<List<String>> replacements, String text) {
123+
for (List<String> e : replacements) {
124+
d.put(e.get(0), e.get(1));
125+
}
126+
return dfs(text);
127+
}
128+
129+
private String dfs(String s) {
130+
int i = s.indexOf("%");
131+
if (i == -1) {
132+
return s;
133+
}
134+
int j = s.indexOf("%", i + 1);
135+
if (j == -1) {
136+
return s;
137+
}
138+
String key = s.substring(i + 1, j);
139+
String replacement = dfs(d.getOrDefault(key, ""));
140+
return s.substring(0, i) + replacement + dfs(s.substring(j + 1));
141+
}
142+
}
93143
```
94144

95145
#### C++
96146

97147
```cpp
98-
148+
class Solution {
149+
public:
150+
string applySubstitutions(vector<vector<string>>& replacements, string text) {
151+
unordered_map<string, string> d;
152+
for (const auto& e : replacements) {
153+
d[e[0]] = e[1];
154+
}
155+
auto dfs = [&](this auto&& dfs, const string& s) -> string {
156+
size_t i = s.find('%');
157+
if (i == string::npos) {
158+
return s;
159+
}
160+
size_t j = s.find('%', i + 1);
161+
if (j == string::npos) {
162+
return s;
163+
}
164+
string key = s.substr(i + 1, j - i - 1);
165+
string replacement = dfs(d[key]);
166+
return s.substr(0, i) + replacement + dfs(s.substr(j + 1));
167+
};
168+
return dfs(text);
169+
}
170+
};
99171
```
100172
101173
#### Go
102174
103175
```go
176+
func applySubstitutions(replacements [][]string, text string) string {
177+
d := make(map[string]string)
178+
for _, e := range replacements {
179+
d[e[0]] = e[1]
180+
}
181+
var dfs func(string) string
182+
dfs = func(s string) string {
183+
i := strings.Index(s, "%")
184+
if i == -1 {
185+
return s
186+
}
187+
j := strings.Index(s[i+1:], "%")
188+
if j == -1 {
189+
return s
190+
}
191+
j += i + 1
192+
key := s[i+1 : j]
193+
replacement := dfs(d[key])
194+
return s[:i] + replacement + dfs(s[j+1:])
195+
}
196+
197+
return dfs(text)
198+
}
199+
```
104200

201+
#### TypeScript
202+
203+
```ts
204+
function applySubstitutions(replacements: string[][], text: string): string {
205+
const d: Record<string, string> = {};
206+
for (const [key, value] of replacements) {
207+
d[key] = value;
208+
}
209+
210+
const dfs = (s: string): string => {
211+
const i = s.indexOf('%');
212+
if (i === -1) {
213+
return s;
214+
}
215+
const j = s.indexOf('%', i + 1);
216+
if (j === -1) {
217+
return s;
218+
}
219+
const key = s.slice(i + 1, j);
220+
const replacement = dfs(d[key] ?? '');
221+
return s.slice(0, i) + replacement + dfs(s.slice(j + 1));
222+
};
223+
224+
return dfs(text);
225+
}
105226
```
106227

107228
<!-- tabs:end -->

solution/3400-3499/3481.Apply Substitutions/README_EN.md

+125-4
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,153 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3481.Ap
7676

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

79-
### Solution 1
79+
### Solution 1: Hash Table + Recursion
80+
81+
We use a hash table $\textit{d}$ to store the substitution mapping, and then define a function $\textit{dfs}$ to recursively replace the placeholders in the string.
82+
83+
The execution logic of the function $\textit{dfs}$ is as follows:
84+
85+
1. Find the starting position $i$ of the first placeholder in the string $\textit{s}$. If not found, return $\textit{s}$;
86+
2. Find the ending position $j$ of the first placeholder in the string $\textit{s}$. If not found, return $\textit{s}$;
87+
3. Extract the key of the placeholder, and then recursively replace the value of the placeholder $d[key]$;
88+
4. Return the replaced string.
89+
90+
In the main function, we call the $\textit{dfs}$ function, pass in the text string $\textit{text}$, and return the result.
91+
92+
The time complexity is $O(m + n \times L)$, and the space complexity is $O(m + n \times L)$. Where $m$ is the length of the substitution mapping, and $n$ and $L$ are the length of the text string and the average length of the placeholders, respectively.
8093

8194
<!-- tabs:start -->
8295

8396
#### Python3
8497

8598
```python
86-
99+
class Solution:
100+
def applySubstitutions(self, replacements: List[List[str]], text: str) -> str:
101+
def dfs(s: str) -> str:
102+
i = s.find("%")
103+
if i == -1:
104+
return s
105+
j = s.find("%", i + 1)
106+
if j == -1:
107+
return s
108+
key = s[i + 1 : j]
109+
replacement = dfs(d[key])
110+
return s[:i] + replacement + dfs(s[j + 1 :])
111+
112+
d = {s: t for s, t in replacements}
113+
return dfs(text)
87114
```
88115

89116
#### Java
90117

91118
```java
92-
119+
class Solution {
120+
private final Map<String, String> d = new HashMap<>();
121+
122+
public String applySubstitutions(List<List<String>> replacements, String text) {
123+
for (List<String> e : replacements) {
124+
d.put(e.get(0), e.get(1));
125+
}
126+
return dfs(text);
127+
}
128+
129+
private String dfs(String s) {
130+
int i = s.indexOf("%");
131+
if (i == -1) {
132+
return s;
133+
}
134+
int j = s.indexOf("%", i + 1);
135+
if (j == -1) {
136+
return s;
137+
}
138+
String key = s.substring(i + 1, j);
139+
String replacement = dfs(d.getOrDefault(key, ""));
140+
return s.substring(0, i) + replacement + dfs(s.substring(j + 1));
141+
}
142+
}
93143
```
94144

95145
#### C++
96146

97147
```cpp
98-
148+
class Solution {
149+
public:
150+
string applySubstitutions(vector<vector<string>>& replacements, string text) {
151+
unordered_map<string, string> d;
152+
for (const auto& e : replacements) {
153+
d[e[0]] = e[1];
154+
}
155+
auto dfs = [&](this auto&& dfs, const string& s) -> string {
156+
size_t i = s.find('%');
157+
if (i == string::npos) {
158+
return s;
159+
}
160+
size_t j = s.find('%', i + 1);
161+
if (j == string::npos) {
162+
return s;
163+
}
164+
string key = s.substr(i + 1, j - i - 1);
165+
string replacement = dfs(d[key]);
166+
return s.substr(0, i) + replacement + dfs(s.substr(j + 1));
167+
};
168+
return dfs(text);
169+
}
170+
};
99171
```
100172
101173
#### Go
102174
103175
```go
176+
func applySubstitutions(replacements [][]string, text string) string {
177+
d := make(map[string]string)
178+
for _, e := range replacements {
179+
d[e[0]] = e[1]
180+
}
181+
var dfs func(string) string
182+
dfs = func(s string) string {
183+
i := strings.Index(s, "%")
184+
if i == -1 {
185+
return s
186+
}
187+
j := strings.Index(s[i+1:], "%")
188+
if j == -1 {
189+
return s
190+
}
191+
j += i + 1
192+
key := s[i+1 : j]
193+
replacement := dfs(d[key])
194+
return s[:i] + replacement + dfs(s[j+1:])
195+
}
196+
197+
return dfs(text)
198+
}
199+
```
104200

201+
#### TypeScript
202+
203+
```ts
204+
function applySubstitutions(replacements: string[][], text: string): string {
205+
const d: Record<string, string> = {};
206+
for (const [key, value] of replacements) {
207+
d[key] = value;
208+
}
209+
210+
const dfs = (s: string): string => {
211+
const i = s.indexOf('%');
212+
if (i === -1) {
213+
return s;
214+
}
215+
const j = s.indexOf('%', i + 1);
216+
if (j === -1) {
217+
return s;
218+
}
219+
const key = s.slice(i + 1, j);
220+
const replacement = dfs(d[key] ?? '');
221+
return s.slice(0, i) + replacement + dfs(s.slice(j + 1));
222+
};
223+
224+
return dfs(text);
225+
}
105226
```
106227

107228
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
string applySubstitutions(vector<vector<string>>& replacements, string text) {
4+
unordered_map<string, string> d;
5+
for (const auto& e : replacements) {
6+
d[e[0]] = e[1];
7+
}
8+
auto dfs = [&](this auto&& dfs, const string& s) -> string {
9+
size_t i = s.find('%');
10+
if (i == string::npos) {
11+
return s;
12+
}
13+
size_t j = s.find('%', i + 1);
14+
if (j == string::npos) {
15+
return s;
16+
}
17+
string key = s.substr(i + 1, j - i - 1);
18+
string replacement = dfs(d[key]);
19+
return s.substr(0, i) + replacement + dfs(s.substr(j + 1));
20+
};
21+
return dfs(text);
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func applySubstitutions(replacements [][]string, text string) string {
2+
d := make(map[string]string)
3+
for _, e := range replacements {
4+
d[e[0]] = e[1]
5+
}
6+
var dfs func(string) string
7+
dfs = func(s string) string {
8+
i := strings.Index(s, "%")
9+
if i == -1 {
10+
return s
11+
}
12+
j := strings.Index(s[i+1:], "%")
13+
if j == -1 {
14+
return s
15+
}
16+
j += i + 1
17+
key := s[i+1 : j]
18+
replacement := dfs(d[key])
19+
return s[:i] + replacement + dfs(s[j+1:])
20+
}
21+
22+
return dfs(text)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
private final Map<String, String> d = new HashMap<>();
3+
4+
public String applySubstitutions(List<List<String>> replacements, String text) {
5+
for (List<String> e : replacements) {
6+
d.put(e.get(0), e.get(1));
7+
}
8+
return dfs(text);
9+
}
10+
11+
private String dfs(String s) {
12+
int i = s.indexOf("%");
13+
if (i == -1) {
14+
return s;
15+
}
16+
int j = s.indexOf("%", i + 1);
17+
if (j == -1) {
18+
return s;
19+
}
20+
String key = s.substring(i + 1, j);
21+
String replacement = dfs(d.getOrDefault(key, ""));
22+
return s.substring(0, i) + replacement + dfs(s.substring(j + 1));
23+
}
24+
}

0 commit comments

Comments
 (0)