Java Solution (Bruteforce) + Another solution based on KMP
\npublic String entityParser(String text) {\n            Map<String,Character> codes = new HashMap<>();\n            codes.put("&quot;",\'"\');\n            cod
```\npublic String entityParser(String text) {\n            Map<String,Character> codes = new HashMap<>();\n            codes.put("&quot;",\'"\');\n            codes.put("&apos;",\'\\\'\');\n            codes.put("&amp;",\'&\');\n            codes.put("&gt;",\'>\');\n            codes.put("&lt;",\'<\');\n            codes.put("&frasl;",\'/\');\n\n            StringBuilder sb = new StringBuilder();\n            for(int i=0;i<text.length();i++){\n                if(text.charAt(i)==\'&\'){\n                    StringBuilder temp = new StringBuilder();\n                    temp.append(text.charAt(i));\n                    i++;\n                    while(i<text.length() && text.charAt(i)!=\';\' && text.charAt(i)!=\'&\'){\n                        temp.append(text.charAt(i));\n                        i++;\n                    }\n                    if(i==text.length()) {sb.append(temp); break;}\n                    if(text.charAt(i)==\'&\'){\n                        sb.append(temp);\n                        i--;\n                    }\n                    if(text.charAt(i) == \';\'){\n                        temp.append(\';\');\n                        sb.append(codes.get(temp.toString())==null?temp.toString():codes.get(temp.toString()));\n                    }\n                }else{\n                    sb.append(text.charAt(i));\n                }\n            }\n            return sb.toString();\n    }\n```\n\n**Another solution**\n\nBased on KMP for searching patterns in the text\n\n```\n    // KMP  is used for substring search\n    /*\n    * Where m is the length of the pattern and n is the length of the text\n    * Naive search algo is O(n*m) whereas KMP is O(n+m)\n    * m -- because we scan the pattern once and create a prefix array of lps\n    * n -- because we scan the text only once while doing the search and dont go back on it\n    * Since we have k such strings such as >,<,& O(k*(m+n))\n    * */\n    public int[] prefixTable(String pattern){\n        char pat[] = pattern.toCharArray();\n        int m = pattern.length();\n        int prefix[] = new  int[m];\n        prefix[0]=0;\n        int j =0, i=1;\n        while(i<m){\n            if(pat[i] == pat[j]){\n                prefix[i] = j+1;\n                i++;\n                j++;\n            }else if(j>0){\n                j = prefix[j-1];\n            }else{\n                prefix[i]=0;\n                i++;\n            }\n        }\n        return prefix;\n    }\n\n    // actual KMP algo implementation\n    public ArrayList<Integer> findOccurrences(String text, String pattern){\n        ArrayList<Integer> res = new ArrayList<>();\n        char t[] = text.toCharArray();\n        char pat[] = pattern.toCharArray();\n        int prefix[] = prefixTable(pattern);\n\n        int i=0, j=0;\n        int n = text.length();\n        int m = pattern.length();\n        while(i<n){\n            if(t[i]==pat[j]){\n                if(j==m-1) {\n                    res.add(i-j);\n                    j=0;\n                }\n                else{\n                    j++;\n                }\n                i++;\n            }else if(j>0){\n                j = prefix[j-1];\n            }else{\n                i++;\n            }\n        }\n        return res;\n    }\n\n    public String entityParser(String text){\n        Map<String,Character> codes = new HashMap<>();\n        codes.put("&quot;",\'"\');\n        codes.put("&apos;",\'\\\'\');\n        codes.put("&amp;",\'&\');\n        codes.put("&gt;",\'>\');\n        codes.put("&lt;",\'<\');\n        codes.put("&frasl;",\'/\');\n        StringBuilder sb = new StringBuilder();\n        Map<Integer, String> freqMap = new TreeMap<>();\n        // we will loop over all the keys in the map, do KMP search for all of them and replace their occurrence with the corresponding value\n        for(String key: codes.keySet()){\n            ArrayList<Integer> occurList = findOccurrences(text,key);\n            for(Integer i: occurList){\n                freqMap.put(i,key);\n            }}\n\n        int prev = 0;\n        for(Integer index: freqMap.keySet()){\n            sb.append(text.substring(prev,index));\n            sb.append(codes.get(freqMap.get(index)));\n            prev = index+freqMap.get(index).length();\n        }\n\n        if(prev == 0){\n            return text;\n        }\n\n        if(prev < text.length()){\n           sb.append(text.substring(prev, text.length()));\n        }\n        return sb.toString();\n    }\n```
[]