Skip to content
Open
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
39 changes: 39 additions & 0 deletions leetcode/1401-1500/1410.HTML-Entity-Parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# [1410.HTML Entity Parser][title]

## Description
**HTML entity parser** is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.

The special characters and their entities for HTML are:

- **Quotation Mark**: the entity is `"` and symbol character is `"`.
- **Single Quote Mark**: the entity is `'` and symbol character is `'`.
- **Ampersand**: the entity is `&` and symbol character is `&`.
- **Greater Than Sign**: the entity is `>` and symbol character is `>`.
- **Less Than Sign**: the entity is `&lt;` and symbol character is `<`.
- **Slash**: the entity is `&frasl;` and symbol character is `/`.

Given the input `text` string to the HTML parser, you have to implement the entity parser.

Return the text after replacing the entities by the special characters.

**Example 1:**

```
Input: text = "&amp; is an HTML entity but &ambassador; is not."
Output: "& is an HTML entity but &ambassador; is not."
Explanation: The parser will replace the &amp; entity by &
```

**Example 2:**

```
Input: text = "and I quote: &quot;...&quot;"
Output: "and I quote: \"...\""
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/html-entity-parser
[me]: https://github.com/kylesliu/awesome-golang-algorithm
13 changes: 11 additions & 2 deletions leetcode/1401-1500/1410.HTML-Entity-Parser/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package Solution

func Solution(x bool) bool {
return x
import (
"strings"
)

func Solution(text string) string {
entities := []string{"&quot;", "&apos;", "&gt;", "&lt;", "&frasl;", "&amp;"}
entityMap := []string{"\"", "'", ">", "<", "/", "&"}
for index, e := range entities {
text = strings.ReplaceAll(text, e, entityMap[index])
}
return text
}
13 changes: 6 additions & 7 deletions leetcode/1401-1500/1410.HTML-Entity-Parser/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "&amp; is an HTML entity but &ambassador; is not.", "& is an HTML entity but &ambassador; is not."},
{"TestCase2", "and I quote: &quot;...&quot;", "and I quote: \"...\""},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading