|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * 1410. HTML Entity Parser |
| 5 | + * |
| 6 | + * 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. |
| 7 | + * The special characters and their entities for HTML are: |
| 8 | + * Quotation Mark: the entity is " and symbol character is ". |
| 9 | + * Single Quote Mark: the entity is ' and symbol character is '. |
| 10 | + * Ampersand: the entity is & and symbol character is &. |
| 11 | + * Greater Than Sign: the entity is > and symbol character is >. |
| 12 | + * Less Than Sign: the entity is < and symbol character is <. |
| 13 | + * Slash: the entity is ⁄ and symbol character is /. |
| 14 | + * Given the input text string to the HTML parser, you have to implement the entity parser. |
| 15 | + * |
| 16 | + * Return the text after replacing the entities by the special characters. |
| 17 | + * |
| 18 | + * Example 1: |
| 19 | + * Input: text = "& is an HTML entity but &ambassador; is not." |
| 20 | + * Output: "& is an HTML entity but &ambassador; is not." |
| 21 | + * Explanation: The parser will replace the & entity by & |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * Input: text = "and I quote: "..."" |
| 25 | + * Output: "and I quote: \"...\"" |
| 26 | + * |
| 27 | + * Example 3: |
| 28 | + * Input: text = "Stay home! Practice on Leetcode :)" |
| 29 | + * Output: "Stay home! Practice on Leetcode :)" |
| 30 | + * |
| 31 | + * Example 4: |
| 32 | + * Input: text = "x > y && x < y is always false" |
| 33 | + * Output: "x > y && x < y is always false" |
| 34 | + * |
| 35 | + * Example 5: |
| 36 | + * Input: text = "leetcode.com⁄problemset⁄all" |
| 37 | + * Output: "leetcode.com/problemset/all" |
| 38 | + * |
| 39 | + * Constraints: |
| 40 | + * 1 <= text.length <= 10^5 |
| 41 | + * The string may contain any possible characters out of all the 256 ASCII characters. |
| 42 | + * */ |
| 43 | +public class _1410 { |
| 44 | + public static class Solution1 { |
| 45 | + public String entityParser(String text) { |
| 46 | + StringBuilder sb = new StringBuilder(); |
| 47 | + for (int i = 0; i < text.length(); i++) { |
| 48 | + if (text.charAt(i) != '&') { |
| 49 | + sb.append(text.charAt(i)); |
| 50 | + } else { |
| 51 | + if (i + 7 <= text.length() && text.substring(i, i + 7).equals("⁄")) { |
| 52 | + sb.append("/"); |
| 53 | + i += 6; |
| 54 | + } else if (i + 6 <= text.length() && text.substring(i, i + 6).equals(""")) { |
| 55 | + sb.append("\""); |
| 56 | + i += 5; |
| 57 | + } else if (i + 6 <= text.length() && text.substring(i, i + 6).equals("'")) { |
| 58 | + sb.append("'"); |
| 59 | + i += 5; |
| 60 | + } else if (i + 5 <= text.length() && text.substring(i, i + 5).equals("&")) { |
| 61 | + sb.append("&"); |
| 62 | + i += 4; |
| 63 | + } else if (i + 4 <= text.length() && text.substring(i, i + 4).equals(">")) { |
| 64 | + sb.append(">"); |
| 65 | + i += 3; |
| 66 | + } else if (i + 4 <= text.length() && text.substring(i, i + 4).equals("<")) { |
| 67 | + sb.append("<"); |
| 68 | + i += 3; |
| 69 | + } else { |
| 70 | + sb.append(text.charAt(i)); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + return sb.toString(); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments