Created
December 2, 2023 09:47
-
-
Save skysign/5fe33165e976db3962e29563b05cd5c1 to your computer and use it in GitHub Desktop.
코데풀유튜브(youtube.com/@codapul) 구독 부탁드립니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import copy | |
candidates: list[str] = [] | |
max_length = 0 | |
def solution(s:str): | |
questionMarkIdxes = [] | |
for idx in range(len(s)): | |
if s[idx] == '?': | |
questionMarkIdxes.append(idx) | |
replace_questionmark(s, questionMarkIdxes) | |
for candi in candidates: | |
indexes = [-1] | |
while True: | |
idx = candi.find('<>', indexes[-1] + 1) | |
if idx == -1: | |
break | |
else: | |
indexes.append(idx) | |
indexes.pop(0) | |
if len(indexes) > 0: | |
palindrome_length(candi, indexes) | |
return max_length | |
def replace_questionmark(s, questionMarkIdxes): | |
if (len(questionMarkIdxes) == 0): | |
candidates.append(s) | |
return | |
idx = questionMarkIdxes.pop(0) | |
s1 = s[:idx] + '<' + s[idx + 1:] | |
replace_questionmark(s1, copy.deepcopy(questionMarkIdxes)) | |
s2 = s[:idx] + '>' + s[idx + 1:] | |
replace_questionmark(s2, copy.deepcopy(questionMarkIdxes)) | |
def palindrome_length(s, indexes: list[int]): | |
global max_length | |
while len(indexes) > 0: | |
idx = indexes.pop(0) | |
ltidx = idx | |
rtidx = idx +1 | |
my_length = 2 | |
ltidx = ltidx -1 | |
rtidx = rtidx +1 | |
while 0 <= ltidx and rtidx < len(s): | |
if s[ltidx] == '<' and s[rtidx] == '>': | |
my_length += 2 | |
ltidx = ltidx - 1 | |
rtidx = rtidx + 1 | |
else: | |
break | |
max_length = max(max_length, my_length) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest import TestCase | |
from main import solution | |
class Test(TestCase): | |
def test1_solution(self): | |
self.assertEqual(4, solution('<><??>>')) | |
def test2_solution(self): | |
self.assertEqual(6, solution('??????')) | |
def test3_solution(self): | |
self.assertEqual(2, solution('<<?')) | |
def test4_solution(self): | |
self.assertEqual(0, solution('>?<')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment