diff --git a/leetcode/1401-1500/1410.HTML-Entity-Parser/README.md b/leetcode/1401-1500/1410.HTML-Entity-Parser/README.md new file mode 100644 index 000000000..b461ed2fc --- /dev/null +++ b/leetcode/1401-1500/1410.HTML-Entity-Parser/README.md @@ -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 `<` and symbol character is `<`. +- **Slash**: the entity is `⁄` 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 = "& is an HTML entity but &ambassador; is not." +Output: "& is an HTML entity but &ambassador; is not." +Explanation: The parser will replace the & entity by & +``` + +**Example 2:** + +``` +Input: text = "and I quote: "..."" +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 diff --git a/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution.go b/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution.go index d115ccf5e..3d24207bd 100755 --- a/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution.go +++ b/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution.go @@ -1,5 +1,14 @@ package Solution -func Solution(x bool) bool { - return x +import ( + "strings" +) + +func Solution(text string) string { + entities := []string{""", "'", ">", "<", "⁄", "&"} + entityMap := []string{"\"", "'", ">", "<", "/", "&"} + for index, e := range entities { + text = strings.ReplaceAll(text, e, entityMap[index]) + } + return text } diff --git a/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution_test.go b/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution_test.go index 14ff50eb4..46404ab95 100755 --- a/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution_test.go +++ b/leetcode/1401-1500/1410.HTML-Entity-Parser/Solution_test.go @@ -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", "& is an HTML entity but &ambassador; is not.", "& is an HTML entity but &ambassador; is not."}, + {"TestCase2", "and I quote: "..."", "and I quote: \"...\""}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }