[Algorithm] 가장 긴 문자열 (javaScript)

swing·2023년 6월 21일
0

[Algorithm]

목록 보기
37/96

문제

N개의 문자열이 입력되면 그 중 가장 긴 문자열을 출력하는 프로그램을 작성하세요.

입력설명

첫 줄에 자연수 N이 주어진다.(3<=N<=30)
두 번째 줄부터 N개의 문자열이 주어진다. 문자열의 길이는 100을 넘지 않습니다. 각 문자열의 길이는 서로 다릅니다.

출력설명

첫 줄에 가장 긴 문자열을 출력한다.

입출력예제

입력
5
teacher
time
student
beautiful
good

출력
beautiful

문제 해결

const solution = (input) => {
  const [N, ...words] = input.split("\n");
  let answer = words[0];
  for (let x of words) {
    if (x.length > answer.length) answer = x;
  }
  return answer;
};

const a = solution("5\nteacher\ntime\nstudent\nbeautiful\ngood");

console.log(a); // beautiful
profile
if(기록📝) 성장🌱

0개의 댓글