본문 바로가기
코딩테스트(알고리즘)/leetCode

[leetCode] 3. Longest Substring Without Repeating Characters (Javascript)

by Cafe Mocha 2022. 6. 26.

(5) Longest Substring Without Repeating Characters - LeetCode

 

Longest Substring Without Repeating Characters - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


접근 : 구현, 문자열

 

Javascript

let input = require("fs")
  .readFileSync("input.txt") //"/dev/stdin"
  .toString()
  .split("\n")
  .map((val) => val.trim());

function solution(input) {
  //input
  let s = input[0];
  //Submit
  s = s.split("");
  let len = s.length;

  let max = 0;
  for (let i = 0; i < len; i++) {
    let temp = [s[i]];
    for (let j = i + 1; j < len; j++) {
      if (temp.includes(s[j])) {
        break;
      } else {
        temp.push(s[j]);
      }
    }
    max = Math.max(max, temp.length);
  }

  console.log(max);
}

solution(input);