HTML Entity Parser Solution with O(n) Time Complexity
IntuitionThe problem requires us to replace HTML entities in the string with their corresponding special characters. The HTML entities are denoted by & followed
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to replace HTML entities in the string with their corresponding special characters. The HTML entities are denoted by & followed by the name of the entity and ending with a ; . For example, &amp; is replaced by &, &quot; is replaced by ", etc.

Initially, my thought was to iterate through the string character by character and detect whenever an HTML entity starts (i.e., when & is encountered). After detecting it, I would attempt to extract the entire entity (which ends with ;), check if it exists in a predefined set of mappings, and replace it if found. If it is not a recognized entity, I would just keep the character as-is.
# Approach
<!-- Describe your approach to solving the problem. -->
1.Mapping HTML Entities:
The first step is to create a HashMap where each HTML entity (such as &quot;) is mapped to its corresponding character (such as "). This allows us to quickly look up and replace any entity encountered in the string.

2.Iterate Over the String:
The next step is to iterate over each character in the input string.
When an & character is encountered, I check if it might represent an HTML entity. I do this by looking ahead to find the corresponding ; character to determine the bounds of the entity.

3.Check for Entity in the Map:
After identifying the substring between & and ;, I check if it exists in the HashMap. If it does, I append the corresponding character to the result. If it doesn't, I append the original & character and move to the next character.

4.Edge Cases:
If a & is found but no semicolon follows it (e.g., if the entity is malformed), I treat it as an ordinary character and continue.

5.Return the Result:
Once all characters are processed, I return the result as a string with the replacements made.
# Complexity
- Time complexity:
The time complexity of this solution is O(n), where n is the length of the input string. This is because we iterate through the string once and check each character to see if it starts an HTML entity. For each potential entity, we check the substring (which takes constant time in the context of this approach).
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
The space complexity is O(k), where k is the number of distinct HTML entities in the HashMap. The StringBuilder also requires O(n) space to store the result string, but this is a linear space usage and considered as part of the output.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public String entityParser(String text) {
        HashMap<String, String> map=new HashMap<>();
        map.put("&quot;", "\"");
        map.put("&apos;", "'");
        map.put("&amp;", "&");
        map.put("&gt;", ">");
        map.put("&lt;", "<");
        map.put("&frasl;", "/");
        StringBuilder result = new StringBuilder();
        int i=0;
        while(i<text.length()){
            if(text.charAt(i)=='&'){
                int semicolonIndex = -1;
                for (int j = i + 1; j <text.length(); j++) {
                    if (text.charAt(j) == ';') {
                        semicolonIndex = j;
                        break;
                    }
                }
                if(semicolonIndex!=-1){
                    String test=text.substring(i, semicolonIndex + 1);
                    if(map.containsKey(test)){
                        result.append(map.get(test));
                        i=semicolonIndex+1;
                    }else{
                        result.append(text.charAt(i));
                        i++;
                    }
                }else{
                    result.append(text.charAt(i));
                    i++;
                }
            }
            else{
                result.append(text.charAt(i));
                i++;
            }
        }
        return result.toString();
    }
}
```
['Hash Table', 'String', 'Java']