programmers.co.kr/learn/courses/30/lessons/12904
코딩테스트 연습 - 가장 긴 팰린드롬
앞뒤를 뒤집어도 똑같은 문자열을 팰린드롬(palindrome)이라고 합니다. 문자열 s가 주어질 때, s의 부분문자열(Substring)중 가장 긴 팰린드롬의 길이를 return 하는 solution 함수를 완성해 주세요. 예를들
programmers.co.kr
level3
1.
def solution(s):
answer = 0
if len(s) == 1:
return 1
for i in range(len(s)-1):
for j in range(i+1,len(s)+1):
word = s[i:j]
if answer > len(word):
continue
if word[::-1] in s:
answer = len(word)
if answer == len(s):
return answer
return answer
틀린 답. 왜 안되나 왜안되나 했는데 내가 이해한것은
s에 팰린드롬이 포함되어있나를 확인
이었는데 알고보니 그냥 자른 문자열이 팰린드롬인지 확인하면 되는 문제였다..
2.
def solution(s):
answer = 0
if len(s) == 1:
return 1
for i in range(len(s)-1):
for j in range(i+1,len(s)+1):
word = s[i:j]
if answer > len(word):
continue
if word[::-1] == word:
answer = len(word)
if answer == len(s):
return answer
return answer
그래서 그냥 reverse된 문자열이 원래 문자열과 같은지 확인하는 방법으로 바꾸었더니 바로 합격~
'2 > 프로그래머스' 카테고리의 다른 글
프로그래머스 연습 문제 - JadenCase 문자열 만들기 (0) | 2020.10.30 |
---|---|
프로그래머스 연습 문제 - 가장 큰 정사각형 찾기 (0) | 2020.10.29 |
프로그래머스 연습 문제 - 베스트앨범 (0) | 2020.10.28 |
프로그래머스 연습 문제 - 짝지어 제거하기 (0) | 2020.10.27 |
프로그래머스 연습 문제 - 같은 숫자는 싫어 (0) | 2020.10.26 |