중복되지 않은 알파벳으로 이루어진 제일 긴 단어의 길이구하기

이수현·2022년 5월 25일
0

BOJ

목록 보기
4/4

중복되지 않은 알파벳으로 이루어진 제일 긴 단어의 길이구하기

const getLengthOfStr = (str) => {
  // 아래 코드를 작성해주세요.
  // 제일 긴 중북되지 않는 알파벳의 길이 반환
  // Input : 문자열 output : 숫자
  // "abc abc abc" => abc(3) , "st trg" => trg(3)
  let temp1 = [];
  let temp2 = [];
  for (let i = 0; i < str.length; i++) {
    let temp = str[i];
    for (let j = 0; j < temp1.length; j++) {
      if (temp === temp1[j]) {
        if (temp2.length < temp1.length) {
          temp2 = temp1.slice();
        }
        temp1 = temp1.splice(j + 1, temp1.length);
        break;
      }
    }
    temp1.push(temp);
  }
  return temp1.length > temp2.length ? temp1.length : temp2.length;
};

느낀점

중복 판별을 위한 특정 규칙을 찾는 데 시간이 오래 걸렸다.. 생각하는 연습을 많이하자..

0개의 댓글