Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problem: No.3481 #4150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 125 additions & 4 deletions solution/3400-3499/3481.Apply Substitutions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,153 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3481.Ap

<!-- solution:start -->

### 方法一
### 方法一:哈希表 + 递归

我们用一个哈希表 $\textit{d}$ 来存储替换的映射关系,然后定义一个函数 $\textit{dfs}$ 来递归地替换字符串中的占位符。

函数 $\textit{dfs}$ 的执行逻辑如下:

1. 在字符串 $\textit{s}$ 中查找第一个占位符的起始位置 $i$,如果找不到,则返回 $\textit{s}$;
2. 在字符串 $\textit{s}$ 中查找第一个占位符的结束位置 $j$,如果找不到,则返回 $\textit{s}$;
3. 截取占位符的键值 $key$,然后递归地替换占位符的值 $d[key]$;
4. 返回替换后的字符串。

在主函数中,我们调用 $\textit{dfs}$ 函数,传入文本字符串 $\textit{text}$,并返回结果。

时间复杂度 $O(m + n \times L)$,空间复杂度 $O(m + n \times L)$。其中 $m$ 为替换映射的长度,而 $n$ 和 $L$ 分别为文本字符串的长度和占位符的平均长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def applySubstitutions(self, replacements: List[List[str]], text: str) -> str:
def dfs(s: str) -> str:
i = s.find("%")
if i == -1:
return s
j = s.find("%", i + 1)
if j == -1:
return s
key = s[i + 1 : j]
replacement = dfs(d[key])
return s[:i] + replacement + dfs(s[j + 1 :])

d = {s: t for s, t in replacements}
return dfs(text)
```

#### Java

```java

class Solution {
private final Map<String, String> d = new HashMap<>();

public String applySubstitutions(List<List<String>> replacements, String text) {
for (List<String> e : replacements) {
d.put(e.get(0), e.get(1));
}
return dfs(text);
}

private String dfs(String s) {
int i = s.indexOf("%");
if (i == -1) {
return s;
}
int j = s.indexOf("%", i + 1);
if (j == -1) {
return s;
}
String key = s.substring(i + 1, j);
String replacement = dfs(d.getOrDefault(key, ""));
return s.substring(0, i) + replacement + dfs(s.substring(j + 1));
}
}
```

#### C++

```cpp

class Solution {
public:
string applySubstitutions(vector<vector<string>>& replacements, string text) {
unordered_map<string, string> d;
for (const auto& e : replacements) {
d[e[0]] = e[1];
}
auto dfs = [&](this auto&& dfs, const string& s) -> string {
size_t i = s.find('%');
if (i == string::npos) {
return s;
}
size_t j = s.find('%', i + 1);
if (j == string::npos) {
return s;
}
string key = s.substr(i + 1, j - i - 1);
string replacement = dfs(d[key]);
return s.substr(0, i) + replacement + dfs(s.substr(j + 1));
};
return dfs(text);
}
};
```

#### Go

```go
func applySubstitutions(replacements [][]string, text string) string {
d := make(map[string]string)
for _, e := range replacements {
d[e[0]] = e[1]
}
var dfs func(string) string
dfs = func(s string) string {
i := strings.Index(s, "%")
if i == -1 {
return s
}
j := strings.Index(s[i+1:], "%")
if j == -1 {
return s
}
j += i + 1
key := s[i+1 : j]
replacement := dfs(d[key])
return s[:i] + replacement + dfs(s[j+1:])
}

return dfs(text)
}
```

#### TypeScript

```ts
function applySubstitutions(replacements: string[][], text: string): string {
const d: Record<string, string> = {};
for (const [key, value] of replacements) {
d[key] = value;
}

const dfs = (s: string): string => {
const i = s.indexOf('%');
if (i === -1) {
return s;
}
const j = s.indexOf('%', i + 1);
if (j === -1) {
return s;
}
const key = s.slice(i + 1, j);
const replacement = dfs(d[key] ?? '');
return s.slice(0, i) + replacement + dfs(s.slice(j + 1));
};

return dfs(text);
}
```

<!-- tabs:end -->
Expand Down
129 changes: 125 additions & 4 deletions solution/3400-3499/3481.Apply Substitutions/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,153 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3481.Ap

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table + Recursion

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.

The execution logic of the function $\textit{dfs}$ is as follows:

1. Find the starting position $i$ of the first placeholder in the string $\textit{s}$. If not found, return $\textit{s}$;
2. Find the ending position $j$ of the first placeholder in the string $\textit{s}$. If not found, return $\textit{s}$;
3. Extract the key of the placeholder, and then recursively replace the value of the placeholder $d[key]$;
4. Return the replaced string.

In the main function, we call the $\textit{dfs}$ function, pass in the text string $\textit{text}$, and return the result.

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.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def applySubstitutions(self, replacements: List[List[str]], text: str) -> str:
def dfs(s: str) -> str:
i = s.find("%")
if i == -1:
return s
j = s.find("%", i + 1)
if j == -1:
return s
key = s[i + 1 : j]
replacement = dfs(d[key])
return s[:i] + replacement + dfs(s[j + 1 :])

d = {s: t for s, t in replacements}
return dfs(text)
```

#### Java

```java

class Solution {
private final Map<String, String> d = new HashMap<>();

public String applySubstitutions(List<List<String>> replacements, String text) {
for (List<String> e : replacements) {
d.put(e.get(0), e.get(1));
}
return dfs(text);
}

private String dfs(String s) {
int i = s.indexOf("%");
if (i == -1) {
return s;
}
int j = s.indexOf("%", i + 1);
if (j == -1) {
return s;
}
String key = s.substring(i + 1, j);
String replacement = dfs(d.getOrDefault(key, ""));
return s.substring(0, i) + replacement + dfs(s.substring(j + 1));
}
}
```

#### C++

```cpp

class Solution {
public:
string applySubstitutions(vector<vector<string>>& replacements, string text) {
unordered_map<string, string> d;
for (const auto& e : replacements) {
d[e[0]] = e[1];
}
auto dfs = [&](this auto&& dfs, const string& s) -> string {
size_t i = s.find('%');
if (i == string::npos) {
return s;
}
size_t j = s.find('%', i + 1);
if (j == string::npos) {
return s;
}
string key = s.substr(i + 1, j - i - 1);
string replacement = dfs(d[key]);
return s.substr(0, i) + replacement + dfs(s.substr(j + 1));
};
return dfs(text);
}
};
```

#### Go

```go
func applySubstitutions(replacements [][]string, text string) string {
d := make(map[string]string)
for _, e := range replacements {
d[e[0]] = e[1]
}
var dfs func(string) string
dfs = func(s string) string {
i := strings.Index(s, "%")
if i == -1 {
return s
}
j := strings.Index(s[i+1:], "%")
if j == -1 {
return s
}
j += i + 1
key := s[i+1 : j]
replacement := dfs(d[key])
return s[:i] + replacement + dfs(s[j+1:])
}

return dfs(text)
}
```

#### TypeScript

```ts
function applySubstitutions(replacements: string[][], text: string): string {
const d: Record<string, string> = {};
for (const [key, value] of replacements) {
d[key] = value;
}

const dfs = (s: string): string => {
const i = s.indexOf('%');
if (i === -1) {
return s;
}
const j = s.indexOf('%', i + 1);
if (j === -1) {
return s;
}
const key = s.slice(i + 1, j);
const replacement = dfs(d[key] ?? '');
return s.slice(0, i) + replacement + dfs(s.slice(j + 1));
};

return dfs(text);
}
```

<!-- tabs:end -->
Expand Down
23 changes: 23 additions & 0 deletions solution/3400-3499/3481.Apply Substitutions/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
string applySubstitutions(vector<vector<string>>& replacements, string text) {
unordered_map<string, string> d;
for (const auto& e : replacements) {
d[e[0]] = e[1];
}
auto dfs = [&](this auto&& dfs, const string& s) -> string {
size_t i = s.find('%');
if (i == string::npos) {
return s;
}
size_t j = s.find('%', i + 1);
if (j == string::npos) {
return s;
}
string key = s.substr(i + 1, j - i - 1);
string replacement = dfs(d[key]);
return s.substr(0, i) + replacement + dfs(s.substr(j + 1));
};
return dfs(text);
}
};
23 changes: 23 additions & 0 deletions solution/3400-3499/3481.Apply Substitutions/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func applySubstitutions(replacements [][]string, text string) string {
d := make(map[string]string)
for _, e := range replacements {
d[e[0]] = e[1]
}
var dfs func(string) string
dfs = func(s string) string {
i := strings.Index(s, "%")
if i == -1 {
return s
}
j := strings.Index(s[i+1:], "%")
if j == -1 {
return s
}
j += i + 1
key := s[i+1 : j]
replacement := dfs(d[key])
return s[:i] + replacement + dfs(s[j+1:])
}

return dfs(text)
}
24 changes: 24 additions & 0 deletions solution/3400-3499/3481.Apply Substitutions/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
private final Map<String, String> d = new HashMap<>();

public String applySubstitutions(List<List<String>> replacements, String text) {
for (List<String> e : replacements) {
d.put(e.get(0), e.get(1));
}
return dfs(text);
}

private String dfs(String s) {
int i = s.indexOf("%");
if (i == -1) {
return s;
}
int j = s.indexOf("%", i + 1);
if (j == -1) {
return s;
}
String key = s.substring(i + 1, j);
String replacement = dfs(d.getOrDefault(key, ""));
return s.substring(0, i) + replacement + dfs(s.substring(j + 1));
}
}
Loading