본문 바로가기
LeetCode/Stack

[LeetCode][Kotlin] 1544. Make The String Great

by jinwo_o 2024. 11. 27.

1544. Make The String Great

Given a string s of lower and upper case English letters.

 

A good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:

  • 0 <= i <= s.length - 2
  • s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.

To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.

 

Return the string after making it good. The answer is guaranteed to be unique under the given constraints.

 

Notice that an empty string is also good.

소문자와 대문자로 이루어진 문자열 s가 주어졌습니다. 

좋은 문자열은 두 개의 인접한 문자 s[i]와 s[i + 1]이 없는 문자열입니다. 여기서: 
- 0 <= i <= s.length - 2 
- s[i]는 소문자이고 s[i + 1]은 같은 문자이지만 대문자이거나 그 반대입니다. 

문자열을 좋은 문자열로 만들려면 문자열을 나쁜 문자로 만드는 두 개의 인접한 문자를 선택하여 제거할 수 있습니다. 문자열이 좋은 문자열이 될 때까지 이 작업을 계속할 수 있습니다. 

문자열을 좋은 문자열로 만든 후 반환합니다. 주어진 제약 조건 하에서 답은 고유함이 보장됩니다. 

빈 문자열도 좋은 문자열입니다.

 

Example 1:

Input: s = "leEeetcode"

Output: "leetcode"

Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".

 

Example 2:

Input: s = "abBAcC"

Output: ""

Explanation: We have many possible scenarios, and all lead to the same answer. For example:

"abBAcC" --> "aAcC" --> "cC" --> ""

"abBAcC" --> "abBA" --> "aA" --> ""

 

Example 3:

Input: s = "s"

Output: "s"

 

Constraints:

  • 1 <= s.length <= 100
  • s contains only lower and upper case English letters.

코드 1

class Solution {
    fun makeGood(s: String): String {
        val stack = mutableListOf<Char>()

        for (i in s.indices) {
            if (s[i].isUpperCase()) {
                if (stack.isNotEmpty() && stack.last() == s[i].toLowerCase()) {
                    stack.removeLast()
                } else {
                    stack.add(s[i])
                }
            } else {
                if (stack.isNotEmpty() && stack.last() == s[i].uppercaseChar()) {
                    stack.removeLast()
                } else {
                    stack.add(s[i])
                }
            }
        }

        return stack.joinToString("")
    }
}

 

코드 2

class Solution {
    fun makeGood(s: String): String {
        fun lower(c: Char): Char {
            if (c in 'A'..'Z') {
                return (c - 'A' + 'a'.code).toChar()
            }
            return c
        }

        val stack = mutableListOf<Char>()
        var i = 0
        while (i < s.length) {
            if (stack.isNotEmpty() &&
                stack.last() != s[i] &&
                lower(stack.last()) == lower(s[i])
            ) {
                stack.removeLast()
            } else {
                stack.add(s[i])
            }
            i++
        }

        return stack.joinToString("")
    }
}

'LeetCode > Stack' 카테고리의 다른 글

[LeetCode][Kotlin] 853. Car Fleet  (0) 2024.11.29
[LeetCode][Kotlin] 735. Asteroid Collision  (0) 2024.11.29
[LeetCode][Kotlin] 22. Generate Parentheses  (0) 2024.11.28