g1401_1500.s1410_html_entity_parser.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1401_1500.s1410_html_entity_parser
// #Medium #String #Hash_Table #2023_06_07_Time_334_ms_(100.00%)_Space_38.6_MB_(100.00%)
class Solution {
fun entityParser(text: String): String {
val map: MutableMap = HashMap()
map["""] = "\""
map["'"] = "'"
map["&"] = "&"
map[">"] = ">"
map["<"] = "<"
map["⁄"] = "/"
val n = text.length
val sb = StringBuilder()
var i = 0
while (i < n) {
val c = text[i]
if (c == '&') {
val index = text.indexOf(";", i)
if (index >= 0) {
val pattern = text.substring(i, index + 1)
if (map.containsKey(pattern)) {
sb.append(map[pattern])
i += pattern.length
continue
}
}
}
sb.append(c)
i++
}
return sb.toString()
}
}