-
[leetcode] 3. Longest Substring Without Repeating Characters (Typescript)Algorithm 2021. 2. 20. 14:14728x90
Given a string s, find the length of the longest substring without repeating characters
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.
Example 4:
Input: s = ""
Output: 0
Constraints:
- 0 <= s.length <= 5 * 104
- s consists of English letters, digits, symbols and spaces.
function lengthOfLongestSubstring(s: string): number { let left = 0; let right = 0; let set = new Set(); let result = 0; while (right < s.length) { if (set.has(s.charAt(right))) { set.delete(s.charAt(left)); left++; } else { set.add(s.charAt(right)); result = Math.max(result, set.size); right++; } } return result; };
728x90'Algorithm' 카테고리의 다른 글
[Leetcode] 5. Longest palindromic substring (Typescript) (0) 2022.02.01 [leetcode] 4. Median of Two Sorted Arrays (Typescript) (0) 2021.02.21 [Algorithm] Sliding Window 알고리즘 (Javascript) (0) 2021.02.14 [leetcode] 2. Add two numbers (Typescript) (0) 2021.02.13 [leetcode] 1. Two Sum (Typescript) (0) 2021.02.12