안녕하십니까. Eun🦆입니다.

풀이법 입니다.
코딩테스트 연습 - 가장 긴 팰린드롬
앞뒤를 뒤집어도 똑같은 문자열을 팰린드롬(palindrome)이라고 합니다. 문자열 s가 주어질 때, s의 부분문자열(Substring)중 가장 긴 팰린드롬의 길이를 return 하는 solution 함수를 완성해 주세요. 예를들
programmers.co.kr
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(const char* s) {
int answer_odd = 0;
int answer_even = 0;
//printf("len=%d\n",strlen(s));
// odd
for(int idx=1;idx<strlen(s);idx++)
{
int cnt = 0;
for(int cmp=idx+1;cmp<strlen(s);cmp++)
{
//printf("odd:%c\n",s[cmp]);
if(s[cmp]==s[2*idx-cmp] & 2*idx-cmp>=0) // idx-(cmp-(cmp-idx))
cnt++;
else
break;
}
if(answer_odd<cnt)
answer_odd = cnt;
}
//even
for(int idx=0;idx<strlen(s);idx++)
{
int cnt = 0;
for(int cmp=idx+1;cmp<strlen(s);cmp++)
{
//printf("even:%c\n",s[cmp]);
if(s[cmp]==s[2*idx-cmp+1] & 2*idx-cmp+1>=0) // idx-(cmp-(idx+1))
cnt++;
else
break;
}
if(answer_even<cnt)
answer_even = cnt;
}
answer_odd = answer_odd*2+1;
answer_even = answer_even*2;
if(answer_odd>answer_even)
return answer_odd;
else
return answer_even;
}
'개발 > Coding Test' 카테고리의 다른 글
[프로그래머스 Lv3 C++] 야근 지수 (0) | 2022.06.07 |
---|---|
[프로그래머스 Lv3 C++] 멀리 뛰기 (0) | 2022.06.07 |
[프로그래머스 Lv3 C++] 이중우선큐 (0) | 2022.06.07 |
[프로그래머스 Lv3 C++] 단어 변환 (0) | 2022.06.07 |
[프로그래머스 Lv3 C++] 정수 삼각형 (0) | 2022.06.07 |
댓글