Skip to content
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

feat: add solutions to lc problem: No.1410 #2002

Merged
merged 1 commit into from
Nov 23, 2023
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
111 changes: 97 additions & 14 deletions solution/1400-1499/1410.HTML Entity Parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@

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

**方法一:哈希表**
**方法一:哈希表 + 模拟**

我们可以使用哈希表来存储每个字符实体对应的字符,然后遍历字符串,当遇到字符实体时,我们就将其替换为对应的字符。

时间复杂度 $O(n \times l)$,空间复杂度 $O(l)$。其中 $n$ 是字符串的长度,而 $l$ 是字符实体的总长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -124,20 +128,20 @@ class Solution {
int i = 0;
int n = text.length();
while (i < n) {
boolean find = false;
boolean found = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
String t = text.substring(i, j);
if (d.containsKey(t)) {
ans.append(d.get(t));
i = j;
find = true;
found = true;
break;
}
}
}
if (!find) {
if (!found) {
ans.append(text.charAt(i++));
}
}
Expand All @@ -152,38 +156,117 @@ class Solution {
class Solution {
public:
string entityParser(string text) {
unordered_map<string, string> d;
d["&quot;"] = "\"";
d["&apos;"] = "'";
d["&amp;"] = "&";
d["&gt;"] = ">";
d["&lt;"] = "<";
d["&frasl;"] = "/";
unordered_map<string, string> d = {
{"&quot;", "\""},
{"&apos;", "'"},
{"&amp;", "&"},
{"&gt;", ">"},
{"&lt;", "<"},
{"&frasl;", "/"},
};
string ans = "";
int i = 0, n = text.size();
while (i < n) {
bool find = false;
bool found = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
string t = text.substr(i, l);
if (d.count(t)) {
ans += d[t];
i = j;
find = true;
found = true;
break;
}
}
}
if (!find) ans += text[i++];
if (!found) ans += text[i++];
}
return ans;
}
};
```

### **Go**

```go
func entityParser(text string) string {
d := map[string]string{
"&quot;": "\"",
"&apos;": "'",
"&amp;": "&",
"&gt;": ">",
"&lt;": "<",
"&frasl;": "/",
}
var ans strings.Builder
i, n := 0, len(text)

for i < n {
found := false
for l := 1; l < 8; l++ {
j := i + l
if j <= n {
t := text[i:j]
if val, ok := d[t]; ok {
ans.WriteString(val)
i = j
found = true
break
}
}
}
if !found {
ans.WriteByte(text[i])
i++
}
}

return ans.String()
}
```

### **TypeScript**

```ts
function entityParser(text: string): string {
const d: Record<string, string> = {
'&quot;': '"',
'&apos;': "'",
'&amp;': '&',
'&gt;': '>',
'&lt;': '<',
'&frasl;': '/',
};

let ans: string = '';
let i: number = 0;
const n: number = text.length;

while (i < n) {
let found: boolean = false;
for (let l: number = 1; l < 8; ++l) {
const j: number = i + l;
if (j <= n) {
const t: string = text.substring(i, j);
if (d.hasOwnProperty(t)) {
ans += d[t];
i = j;
found = true;
break;
}
}
}

if (!found) {
ans += text[i++];
}
}

return ans;
}
```

```ts
function entityParser(text: string): string {
const d: { [key: string]: string } = {
Expand Down
111 changes: 98 additions & 13 deletions solution/1400-1499/1410.HTML Entity Parser/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@

## Solutions

**Solution 1: Hash Table + Simulation**

We can use a hash table to store the corresponding character for each character entity. Then, we traverse the string, and when we encounter a character entity, we replace it with the corresponding character.

The time complexity is $O(n \times l)$, and the space complexity is $O(l)$. Here, $n$ is the length of the string, and $l$ is the total length of the character entities.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -93,20 +99,20 @@ class Solution {
int i = 0;
int n = text.length();
while (i < n) {
boolean find = false;
boolean found = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
String t = text.substring(i, j);
if (d.containsKey(t)) {
ans.append(d.get(t));
i = j;
find = true;
found = true;
break;
}
}
}
if (!find) {
if (!found) {
ans.append(text.charAt(i++));
}
}
Expand All @@ -121,38 +127,117 @@ class Solution {
class Solution {
public:
string entityParser(string text) {
unordered_map<string, string> d;
d["&quot;"] = "\"";
d["&apos;"] = "'";
d["&amp;"] = "&";
d["&gt;"] = ">";
d["&lt;"] = "<";
d["&frasl;"] = "/";
unordered_map<string, string> d = {
{"&quot;", "\""},
{"&apos;", "'"},
{"&amp;", "&"},
{"&gt;", ">"},
{"&lt;", "<"},
{"&frasl;", "/"},
};
string ans = "";
int i = 0, n = text.size();
while (i < n) {
bool find = false;
bool found = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
string t = text.substr(i, l);
if (d.count(t)) {
ans += d[t];
i = j;
find = true;
found = true;
break;
}
}
}
if (!find) ans += text[i++];
if (!found) ans += text[i++];
}
return ans;
}
};
```

### **Go**

```go
func entityParser(text string) string {
d := map[string]string{
"&quot;": "\"",
"&apos;": "'",
"&amp;": "&",
"&gt;": ">",
"&lt;": "<",
"&frasl;": "/",
}
var ans strings.Builder
i, n := 0, len(text)

for i < n {
found := false
for l := 1; l < 8; l++ {
j := i + l
if j <= n {
t := text[i:j]
if val, ok := d[t]; ok {
ans.WriteString(val)
i = j
found = true
break
}
}
}
if !found {
ans.WriteByte(text[i])
i++
}
}

return ans.String()
}
```

### **TypeScript**

```ts
function entityParser(text: string): string {
const d: Record<string, string> = {
'&quot;': '"',
'&apos;': "'",
'&amp;': '&',
'&gt;': '>',
'&lt;': '<',
'&frasl;': '/',
};

let ans: string = '';
let i: number = 0;
const n: number = text.length;

while (i < n) {
let found: boolean = false;
for (let l: number = 1; l < 8; ++l) {
const j: number = i + l;
if (j <= n) {
const t: string = text.substring(i, j);
if (d.hasOwnProperty(t)) {
ans += d[t];
i = j;
found = true;
break;
}
}
}

if (!found) {
ans += text[i++];
}
}

return ans;
}
```

```ts
function entityParser(text: string): string {
const d: { [key: string]: string } = {
Expand Down
61 changes: 31 additions & 30 deletions solution/1400-1499/1410.HTML Entity Parser/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
class Solution {
public:
string entityParser(string text) {
unordered_map<string, string> d;
d["&quot;"] = "\"";
d["&apos;"] = "'";
d["&amp;"] = "&";
d["&gt;"] = ">";
d["&lt;"] = "<";
d["&frasl;"] = "/";
string ans = "";
int i = 0, n = text.size();
while (i < n) {
bool find = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
string t = text.substr(i, l);
if (d.count(t)) {
ans += d[t];
i = j;
find = true;
break;
}
}
}
if (!find) ans += text[i++];
}
return ans;
}
class Solution {
public:
string entityParser(string text) {
unordered_map<string, string> d = {
{"&quot;", "\""},
{"&apos;", "'"},
{"&amp;", "&"},
{"&gt;", ">"},
{"&lt;", "<"},
{"&frasl;", "/"},
};
string ans = "";
int i = 0, n = text.size();
while (i < n) {
bool found = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
string t = text.substr(i, l);
if (d.count(t)) {
ans += d[t];
i = j;
found = true;
break;
}
}
}
if (!found) ans += text[i++];
}
return ans;
}
};
Loading