Skip to content

Commit 40bb31d

Browse files
Add Longest Substring Without Repeating Characters
1 parent 40403e0 commit 40bb31d

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Longest Substring Without Repeating Characters
2+
3+
## LeetCode Problem
4+
- Problem Number: 3
5+
- Problem Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
6+
- Difficulty: Medium
7+
- Topics: Hash Table, String, Sliding Window
8+
9+
## Problem Description
10+
Given a string s, find the length of the longest substring without repeating characters.
11+
12+
Note:
13+
- A substring is a contiguous sequence of characters within the string
14+
- The substring must not have any repeating characters
15+
16+
## Examples
17+
18+
### Example 1:
19+
```
20+
Input: s = "abcabcbb"
21+
Output: 3
22+
Explanation: The answer is "abc", with the length of 3.
23+
```
24+
25+
### Example 2:
26+
```
27+
Input: s = "bbbbb"
28+
Output: 1
29+
Explanation: The answer is "b", with the length of 1.
30+
```
31+
32+
### Example 3:
33+
```
34+
Input: s = "pwwkew"
35+
Output: 3
36+
Explanation: The answer is "wke", with the length of 3.
37+
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
38+
```
39+
40+
## Constraints
41+
- 0 <= s.length <= 5 * 10⁴
42+
- s consists of English letters, digits, symbols and spaces
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.HashSet;
2+
3+
class Solution {
4+
public int lengthOfLongestSubstring(String s) {
5+
if (s == null || s.length() == 0) {
6+
return 0;
7+
}
8+
9+
HashSet<Character> set = new HashSet<>();
10+
int maxLen = 0;
11+
int left = 0;
12+
13+
for (int right = 0; right < s.length(); right++) {
14+
char currentChar = s.charAt(right);
15+
16+
while (set.contains(currentChar)) {
17+
set.remove(s.charAt(left));
18+
left++;
19+
}
20+
21+
set.add(currentChar);
22+
23+
maxLen = Math.max(maxLen, right - left + 1);
24+
}
25+
26+
return maxLen;
27+
}
28+
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ Each problem has its own directory containing:
1414
|---|-------|------------|----------|---------|
1515
| 1 | [Two Sum](Two%20Sum) | Easy | [Java](Two%20Sum/Solution.java) | Array, Hash Table |
1616
| 2 | [Add Two Numbers](Add%20Two%20Numbers) | Medium | [Java](Add%20Two%20Numbers/Solution.java) | Linked List, Math, Recursion |
17+
| 3 | [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters) | Medium | [Java](Longest%20Substring%20Without%20Repeating%20Characters/Solution.java) | Hash Table, String, Sliding Window |
1718
| 4 | [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays) | Hard | [Java](Median%20of%20Two%20Sorted%20Arrays/Solution.java) | Array, Binary Search, Divide and Conquer |
1819
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
1920

2021
## Categories
2122

2223
### By Difficulty
2324
- Easy: 1
24-
- Medium: 2
25+
- Medium: 3
2526
- Hard: 1
2627

2728
### By Topics
@@ -30,6 +31,9 @@ Each problem has its own directory containing:
3031
- [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays)
3132
- Hash Table Problems
3233
- [Two Sum](Two%20Sum)
34+
- [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters)
35+
- String Problems
36+
- [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters)
3337
- Linked List Problems
3438
- [Add Two Numbers](Add%20Two%20Numbers)
3539
- Math Problems

0 commit comments

Comments
 (0)