Written by
최태열
on
on
[Python][프로그래머스][Level3] 가장 긴 팰린드롬
[Python][프로그래머스][Level3] 가장 긴 팰린드롬
코드
def solution(s):
answer=0
for start in range(len(s)-1):
end = start+1
while end<len(s) and start>=0:
if s[start]==s[end]:
answer=max(answer,end-start+1)
else:
break
start-=1
end+=1
for start in range(len(s)-2):
end = start+2
while end<len(s) and start>=0:
if s[start]==s[end]:
answer=max(answer,end-start+1)
else:
break
start-=1
end+=1
return answer if answer!=0 else 1
주어진 문자열의 지점에서 양쪽으로 나아가면서 같은지 확인한다.
팰린드롬이 홀수인 경우와 짝수인 경우, 두 가지가 있기 때문에
두 가지 경우 모두 고려해서 두개의 while문으로 풀었다.
Discussion and feedback