記錄

3. Longest Substring Without Repeating Characters 본문

FRONTEND STUDY/LeetCode

3. Longest Substring Without Repeating Characters

prts 2022. 11. 5. 22:13

문제 링크: https://leetcode.com/problems/longest-substring-without-repeating-characters/
난이도: Medium

 

Given a string s, find the length of the longest substring without repeating characters.

주어진 문자열 s에서 반복되는 문자가 없는 가장 긴 문자열의 길이를 구하시오.

 

Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

Constraints:

  • 0 <= s.length <= 5 * 104
  • s consists of English letters, digits, symbols and spaces.

Related Topics: Hash Table, String, Sliding Window

 


문제풀이

 

var lengthOfLongestSubstring = function(s) {
    //반복되지 않는 가장 긴 문자열
    let k=0;
    let maxl=0;
    for (let i=0;i<s.length;i++){
        for (let j=k;j<i;j++){ 
            if (s[i]==s[j]){ //중복 문자를 만났을 때
                k=j+1;//그 다음 문자(그 전은 안되니까 넘기고 break)
                break;
            }
        }
        //시작점 k, index+1
        if (i-k+1>maxl){
            maxl=i-k+1;
        }
    }
    return maxl;
};

 

'FRONTEND STUDY > LeetCode' 카테고리의 다른 글

48. Rotate Image  (0) 2022.11.19
28. Find the Index of the First Occurrence in a String  (0) 2022.10.28
20. Valid Parentheses  (0) 2022.10.25
120. Triangle  (0) 2022.10.24
[JS] 386. Lexicographical Numbers  (0) 2022.10.20
Comments