LeetCode 솔루션 분류

[11/8] 1544. Make The String Great

컨텐츠 정보

본문

Easy
175077Add to ListShare

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.

 

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.
Accepted
104,019
Submissions
166,432
태그

관련자료

댓글 2

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 68 ms, faster than 53.21% of Python3 online submissions for Make The String Great.
Memory Usage: 13.9 MB, less than 15.06% of Python3 online submissions for Make The String Great.
class Solution:
    def makeGood(self, s: str) -> str:
        stack = []
        
        for char in list(s):
            lower = char.lower()
            upper = char.upper()
            if stack and (stack[-1] == lower and char == upper or stack[-1] == upper and char == lower):
                stack.pop()
            else:
                stack.append(char)
        
        return "".join(stack)

jihoonjohnkim님의 댓글

  • 익명
  • 작성일
JAVA by jihoonjohnkim
class Solution {
    public String makeGood(String s) {
        for(int j=0; j<s.length()-1;j++){
            if(s.charAt(j)+0==s.charAt(j+1)-32 || s.charAt(j)+0==s.charAt(j+1)+32){
                String str ="";
                str += s.charAt(j);
                str += s.charAt(j+1);
                s=s.replaceAll(str,"");
                j=-1;
            }
        }     
        return s;
    }
}
LeetCode 솔루션 357 / 5 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 860 명
  • 오늘 방문자 4,966 명
  • 어제 방문자 6,975 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,537 명
알림 0