Skip to content

Latest commit

 

History

History
25 lines (22 loc) · 628 Bytes

1544.make-the-string-great.md

File metadata and controls

25 lines (22 loc) · 628 Bytes
fun makeGood(s: String): String {
    if (s.isEmpty()) return s
    val stack = Stack<Char>()
    for (c in s) {
        if (stack.isNotEmpty() && isBad(c, stack.peek()))
            stack.pop()
        else 
            stack.push(c)
    }

    val result = LinkedList<Char>()
    while (stack.isNotEmpty()) {
        result.addFirst(stack.pop())
    }
    return result.joinToString("")
}

private fun isBad(c1: Char, c2: Char): Boolean {
    // Or abs(c1 - c2) == 32
    return c1 - 'a' == c2 - 'A' || c1 - 'A' == c2 - 'a'
}