Develope/Algorithm

[TIL] Algorithm 3

JinDevT 2020. 5. 19. 11:32

1. 문제

 

2. 결과

const getLengthOfStr = str => {
  if(!str){return 0}
  let arr = [];
  let sliced = "";
  for (let i in str) {
    if (sliced.includes(str[i])) {
      //for문을 돌면서 만약 같은 알파벳이 있다면
      sliced = sliced.slice(sliced.indexOf(str[i]) + 1);
    }//빈문자열인 sliced for문을 돌면서 하나씩 잘라준다.
    sliced += str[i];
    arr.push(sliced.length);
  }
  return Math.max(...arr); // 14
}

getLengthOfStr("abcdefghcijklmnop");