[LeetCode] Longest Substring Without Repeating Characters | 투포인터 | 파이썬 알고리즘 인터뷰, 정답, 소스코드
박상길, 『파이썬 알고리즘 인터뷰』를 정리한 내용입니다.
https://leetcode.com/problems/longest-substring-without-repeating-characters/
Longest Substring Without Repeating Characters - LeetCode
Can you solve this real interview question? Longest Substring Without Repeating Characters - 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 "
leetcode.com
import collections
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
answer = 0
left, right = 0, 1
if s == "":
return 0
if len(s) == 1:
return 1
used = collections.defaultdict(int)
used[s[left]] += 1
while right < len(s):
while used[s[right]] > 0:
used[s[left]] -= 1
left += 1
used[s[right]] += 1
right += 1
answer = max(answer, right - left)
return answer
[프로그래머스] 258712. 가장 많이 받은 선물| 구현 | 파이썬, 소스코드, 정답 (1) | 2024.03.26 |
---|---|
[LeetCode] Combination Sum | DFS | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (2) | 2023.06.22 |
[LeetCode] Remove Duplicate Letters | stack | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (0) | 2023.06.15 |
[프로그래머스] 148653. 마법의 엘리베이터| deque | 파이썬, 소스코드, 정답 (0) | 2023.06.14 |
[LeetCode] 3Sum | 투포인터 | 파이썬 알고리즘 인터뷰, 정답, 소스코드 (1) | 2023.06.09 |