SeangG
def _(): _()
SeangG
전체 방문자
오늘
어제
  • 분류 전체보기 (37)
    • Programming Language (0)
      • Python (0)
      • Web (0)
    • Algorithm (34)
    • Art (0)
      • 3D Modeling (0)
      • Pixel (0)
      • Picture (0)
    • Game (0)
    • Project (3)
      • Problems (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • BFS
  • Fast Refresh
  • Tree
  • Daily Question
  • Leecode
  • spring boot
  • 로마 숫자
  • 매핑 테이블
  • LeedCode
  • dfs
  • Python
  • github oauth
  • leetcode
  • WSL
  • react.js
  • string
  • next.js
  • Queue
  • 문자열
  • graph

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
SeangG

def _(): _()

Algorithm

[LeetCode] 1544. Make The String Great (Daily Question)

2024. 4. 5. 11:18

문제


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.

 

주어진 문자열을 좋은 문자열로 만드는 문제이다.

좋은 문자열이란, 같은 문자가 대소문자로 붙어있지 않은 문자열이다.

 

좋은 문자열의 예시 : "aBc", "absdbahsdba", "AbaB"

안 좋은 문자열의 예시 : "aA", "bcCAB", "bbSsSaddd"

 

좋은 문자열을 만들기 위해서는 대소문자로 붙어있는 부분을 없애주어야 한다.

 

주어진 문자열을 좋은 문자열로 만드는 예시는 다음과 같다.

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

 

예시를 보면 없애야 하는 부분을 없앴더니 새로운 부분이 생긴 것을 볼 수 있다.


 

풀이


아이디어

알파벳은 26개로, 같은 알파벳이 대소문자로 붙어있는 경우는 26*2(소문자+대문자, 대문자+소문자), 32가지 밖에 없다.

주어지는 문자열의 길이도 최대 100으로, 짧기 때문에 

없애야 하는 부분을 미리 정의해 두고 문자열이 더 이상 바뀌지 않을 때까지 반복하면 좋은 문자열이 될 것이다.

 

코드

class Solution:
    def makeGood(self, s: str) -> str:

        # 없애야 하는 부분을 미리 정의
        lower = [chr(c) for c in range(ord('a'), ord('z')+1)]
        upper = [chr(c) for c in range(ord('A'), ord('Z')+1)]

        cases = (
            list(map(lambda x: x[0]+x[1],zip(lower, upper))) 
            + list(map(lambda x: x[0]+x[1],zip(upper, lower)))
        )

        s_before = ""

        # 없앨 부분이 더이상 없을 때까지 반복
        while s != s_before:
            s_before = s

            # 없앨 부분을 삭제
            for case in cases:
                s = s.replace(case, "")
    
        return s

 

후기


난이도

전반적으로 무난하게 쉬운 문제이다.

 

없애야 하는 부분을 미리 정의한다는 아이디어를 떠올리지 않아도 그냥 평범하게 푸는 방식도 있기 때문에

어떻게 풀지 생각하는 건 어렵지 않을 것이다.


다만, "abBA"에서 "bB"를 없애면 "aA"가 생기는 것처럼 도중에 없애야 하는 부분이 새로 생기는 부분만 신경 쓰면 어렵지 않다.

 

 

다른 풀이

while로 index를 증가시키며 일일이 확인하는 방법이 있다.없애야 하는 부분이 생기면 index를 1 줄여 처리하면 된다.

만약 s의 길이가 많이 길어지면 이 방식이 더 빠를 것이다.

'Algorithm' 카테고리의 다른 글

[LeetCode] 1249. Minimum Remove to Make Valid Parentheses (Daily Question)  (0) 2024.04.06
[LeetCode] 10. Regular Expression Matching  (0) 2024.04.05
[LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings  (0) 2024.04.04
[LeetCode] 1614. Maximum Nesting Depth of the Parentheses (Daily Question)  (0) 2024.04.04
[LeetCode] 79. Word Search (Daily Question)  (0) 2024.04.03
    'Algorithm' 카테고리의 다른 글
    • [LeetCode] 1249. Minimum Remove to Make Valid Parentheses (Daily Question)
    • [LeetCode] 10. Regular Expression Matching
    • [LeetCode] 1111. Maximum Nesting Depth of Two Valid Parentheses Strings
    • [LeetCode] 1614. Maximum Nesting Depth of the Parentheses (Daily Question)
    SeangG
    SeangG

    티스토리툴바