๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ์œ ํ˜•์˜ ๋ฌธ์ œ๋ฅผ ๋” ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค. ๋”ฑ ๋ฌธ์ œ๋งŒ ๋ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผํ•  ์ง€ ๊ฐ์ด ์•ˆ ์žกํžŒ๋‹ค. ๐Ÿ˜ข

 

์œ ํšจํ•œ ํŒฐ๋ฆฐ๋“œ๋กฌ


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

def isPalindrome(self, s: str) -> bool:
    filter_string = ""
    for a in s.lower():
        if a.isalnum():
            filter_string += a
    return filter_string == filter_string[::-1]

ํ’€์ด1. ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜

def isPalindrome(self, s: str) -> bool:
    strs = []
    for char in s:
        if char.isalnum():
            strs.append(char.lower())

    # ํŒฐ๋ฆฐ๋“œ๋กฌ ํŒ๋ณ„ ์—ฌ๋ถ€
    while len(strs) > 1:
        if strs.pop(0) != strs.pop():
            return False
    return True

ํ’€์ด2. Deque ์ž๋ฃŒํ˜•์„ ์ด์šฉํ•œ ์ตœ์ ํ™”

def isPalindrome(self, s: str) -> bool:
    strs: Deque = collections.deque()

    for char in s:
        if char.isalnum():
            strs.append(char.lower())

    while len(strs) > 1:
        if strs.popleft() !== strs.pop():
            return False

    return True

ํ’€์ด3. ์Šฌ๋ผ์ด์‹ฑ ์‚ฌ์šฉ

def isPalindrome(self, s: str) -> bool:
    s = s.lower()
    # ์ •๊ทœ์‹์œผ๋กœ ๋ถˆํ•„์š”ํ•œ ๋ฌธ์ž ํ•„ํ„ฐ๋ง
    s = re.sub('[^a-z0-9]', '', s)
    return s == s[::-1] # ์Šฌ๋ผ์ด์‹ฑ

 

๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

def reverseString(self, s: List[str]) -> None:
    s.reverse()

ํ’€์ด1. ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์ด์šฉํ•œ ์Šค์™‘

def reverseString(self, s: List[str]) -> None:
    left, right = 0, len(s) - 1
    while left < right:
        s[left], s[right] = s[right], s[left]
        left += 1
        right -= 1

ํ’€์ด2. ํŒŒ์ด์ฌ๋‹ค์šด ๋ฐฉ์‹

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์™€ ๋™์ผ

 

๋กœ๊ทธ ํŒŒ์ผ ์žฌ์ •๋ ฌ

๐Ÿ’ก 2๊ฐœ ์ด์ƒ์˜ ์กฐ๊ฑด์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ ๋žŒ๋‹ค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

ํ’€์ด1. ๋žŒ๋‹ค์™€ + ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉ

def reorderLogFiles(self, logs: List[str]) -> List[str]:
    letters, digits = [], []
    for log in logs:
        if log.split()[1].isdigit():
            digits.append(log)
        else:
            letters.append(log)

    # 2๊ฐœ์˜ ํ‚ค๋ฅผ ๋žŒ๋‹ค ํ‘œํ˜„์‹์œผ๋กœ ์ •๋ ฌ
    letters.sort(key=lambda x: (x.split()[1:], x.split()[0]))
    return letters + digits

๊ฐ€์žฅ ํ”ํ•œ ๋‹จ์–ด

๐Ÿ’ก Counter ๊ฐ์ฒด ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ์ˆ™์ง€ํ•˜๊ธฐ

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
    words = re.sub('[^\w]', ' ', paragraph.lower()).split()
    dic = dict()
    for word in words:
        if not word in banned:
            dic[word] = dic.get(word, 0) + 1
    return max(dic.keys(), key=lambda x: dic[x])

ํ’€์ด1. ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜, Counter ๊ฐ์ฒด ์‚ฌ์šฉ

def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
    words = [word for word in re.sub(r'[^\w]', ' ', paragraph).lower().split() if word not in banned]
    counts = collections.Counter(words)
    return counts.most_common(1)[0][0]

 

๊ทธ๋ฃน ์• ๋„ˆ๊ทธ๋žจ

ํ’€์ด1. ์ •๋ ฌํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€

def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
    anagrams = collections.defaultdict(list)

    for word in strs:
        # ์ •๋ ฌํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ์— ์ถ”๊ฐ€
        anagrams[''.join(sorted(word))].append(word)
    return anagrams.values()

 

๊ฐ€์žฅ ๊ธด ํŒฐ๋ฆฐ๋“œ๋กฌ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด

๐Ÿ’ก ํˆฌ ํฌ์ธํ„ฐ ๊ฐœ๋… ์ˆ™์ง€ํ•˜๊ณ  ๋น„์Šทํ•œ ์œ ํ˜•์˜ ๋ฌธ์ œ ํ’€์–ด๋ณด๊ธฐ

ํ’€์ด1. ์ค‘์•™์„ ์ค‘์‹ฌ์œผ๋กœ ํ™•์žฅํ•˜๋Š” ํ’€์ด

def longestPalindrome(self, s: str) -> str:
    # ํŒฐ๋ฆฐ๋“œ๋กฌ ํŒ๋ณ„ ๋ฐ ํˆฌ ํฌ์ธํ„ฐ ํ™•์žฅ
    def expand(left: int, right: int) -> str:
        while left >= 0 and right <= len(s) and s[left] == s[right -1]:
            left -= 1
            right += 1
        return s[left + 1:right - 1]

    # ํ•ด๋‹น ์‚ฌํ•ญ์ด ์—†์„ ๋•Œ ๋น ๋ฅด๊ฒŒ ๋ฆฌํ„ด
    if len(s) < 2 orf s == s[::-1]:
        return s

    result = ''
    # ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์šฐ์ธก์œผ๋กœ ์ด๋™
    for i in range(len(s) - 1):
        result = max(result, 
                        expand(i, i+1),
                        expand(i, i+2),
                        key=len)
    return result
๋ฐ˜์‘ํ˜•