Problem solving/Algorithms

[LeetCode] 1678. Goal Parser Interpretation (Python)

Young_A 2021. 1. 10. 09:55

LeetCode - Problems - Algorithms - 1678. Goal Parser Interpretation

Problem Description

You own a Goal Parser that can interpret a string command. The command consists of an alphabet of "G""()" and/or "(al)" in some order.

The Goal Parser will interpret "G" as the string "G""()" as the string "o", and "(al)" as the string "al".

The interpreted strings are then concatenated in the original order.

 

Given the string command, return the Goal Parser's interpretations of command.

 

Example:

Constraints:

My Solution (Python)

class Solution(object):
    def interpret(self, command):
        """
        :type command: str
        :rtype: str
        """
        command = command.replace("()", "o")
        command = command.replace("(al)", "al")
        return command

์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ replace ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

G๋Š” ๊ทธ๋Œ€๋กœ G๊ฐ€ ๋˜๋ฏ€๋กœ ์ œ์™ธํ•˜๊ณ  "()"->"o" ๊ทธ๋ฆฌ๊ณ  "(al)" -> al ์„ ์œ„ํ•ด ๋‘ ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

return command.replace("()", "o").replace("(al)", "al")

์œ„์™€ ๊ฐ™์ด ํ•œ ์ค„๋กœ๋„ ์ž‘์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค.