Leetcode
2020.04.09 12:52

# 3. Longest Substring Without Repeating Characters

조회 수 113 추천 수 0 댓글 0
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

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

Example 1:

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

Example 2:

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

Example 3:

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

```class Solution {
public int lengthOfLongestSubstring(String s) {
int maxLen = 0;
int loop = s.length();
int start = 0;
int curr = 0;
Set<Character> set = new HashSet<>();
//HashMap<String, Sring> hashMap = new HashMap<String, Sring>();

//qrsvbspk
while(curr < loop){
if(!set.contains(s.charAt(curr))){
maxLen = Math.max(maxLen, set.size());
}else{
set.remove(s.charAt(start++));
}
}

return maxLen;
}
}```

```class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
maxlen = 0
loop = len(s)
start = 0
curr = 0
hset = set()

while (curr < loop):
if not s[curr] in hset:
curr += 1
maxlen = maxlen if maxlen > len(hset) else len(hset)
else:
hset.remove(s[start])
start += 1

return maxlen```

?

 제목+내용제목내용댓글이름닉네임아이디태그
List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
74 Leetcode 692. Top K Frequent Words 2020.04.15 93
73 Leetcode 2. Add Two Numbers 2020.04.14 96
72 Leetcode 731. My Calendar II 2020.04.15 100
71 Leetcode 75. Sort Colors 2020.04.14 101
70 Leetcode 347. Top K Frequent Elements 2020.04.14 102
69 Leetcode 20. Valid Parentheses 2020.04.25 102
68 Leetcode 430. Flatten a Multilevel Doubly Linked List 2020.04.06 104
67 Leetcode 23. Merge k Sorted Lists 2020.04.14 104
66 Leetcode 729. My Calendar I 2020.04.15 104
65 Leetcode 253. Meeting Rooms II 2020.04.15 104
64 Leetcode 852. Peak Index in a Mountain Array 2020.04.28 104
63 Leetcode 7. Reverse Integer 2020.04.28 104
62 Leetcode 62. Unique Paths 2020.04.09 105
61 Leetcode 350. Intersection of Two Arrays II 2020.04.28 106
60 Leetcode 946. Validate Stack Sequences 2020.04.08 107
59 Leetcode 973. K Closest Points to Origin 2020.04.15 107
58 Leetcode 443. String Compression 2020.04.25 107
57 Leetcode 38. Count and Say 2020.04.25 107
56 Leetcode 53. Maximum Subarray 2020.04.28 110
55 Leetcode 937. Reorder Data in Log Files 2020.04.25 111
Board Pagination Prev 1 2 3 4 Next
/ 4