-
프로그래머스 Level 3 > 가장 긴 펠린드롬프로그래머스(Programmers) 2018. 7. 3. 20:40반응형
https://programmers.co.kr/learn/courses/30/lessons/12904
1. 문제
앞뒤를 뒤집어도 똑같은 문자열을 팰린드롬(palindrome)이라고 합니다.
문자열 s가 주어질 때, s의 부분문자열(Substring)중 가장 긴 팰린드롬의 길이를 return 하는 solution 함수를 완성해 주세요.
예를들면, 문자열 s가 abcdcba이면 7을 return하고 abacde이면 3을 return합니다.
제한사항
문자열 s의 길이 : 2500 이하의 자연수
문자열 s는 알파벳 소문자로만 구성
2. 알고리즘
키워드 - 문자열, 성준대리님은 풀었어요
3. 코드
123456789101112131415161718192021222324252627#include <iostream>#include <string>#include <algorithm>using namespace std;int solution(string s){int answer = 0;int length = s.length();for(int i=0;i<length;i++) {for(int subLength = length; subLength >answer; subLength--) {int left = i;int right = left+subLength - 1;while(left<right && s[left] == s[right]) {left++;right--;}if(left >= right && answer < subLength) {answer = subLength;break;}}}return answer;}cs 반응형'프로그래머스(Programmers)' 카테고리의 다른 글
프로그래머스 > Level 1 > 가운데 글자 가져오기 (0) 2018.08.08 프로그래머스 사용자 제작 문제 > 소수의 합 (0) 2018.07.11 프로그래머스 Level 3 > 2 x n 타일링 (0) 2018.07.03 프로그래머스 Level1 > 시저 암호 (0) 2018.07.03 프로그래머스 Level2 > 숫자의 표현 (0) 2018.07.03