Skip to content

Commit 5eb3f90

Browse files
solves longestsubstring with repeating characters
1 parent 226d1e2 commit 5eb3f90

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
|:----:|-----------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------:|
1414
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [![Java](assets/java.png)](src/TwoSum.java) [![Python](assets/python.png)](python/two_sum.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/9wSL_7NN-A8) [![python-yt](assets/python-yt.png)](https://youtu.be/N5FXCTg0TDE) |
1515
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers) | [![Java](assets/java.png)](src/AddTwoNumbers.java) | |
16+
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters) | [![Java](assets/java.png)](src/LongestSubstringWithoutRepeatingCharacters.java) | |
1617
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [![Java](assets/java.png)](src/ReverseInteger.java) [![Python](assets/python.png)](python/reverse_integer.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/7bOhyl5lWjI) [![python-yt](assets/python-yt.png)](https://youtu.be/lmLG30TLcSg) |
1718
| 9 | [PalindromeNumber](https://leetcode.com/problems/palindrome-number/) | [![Java](assets/java.png)](src/PalindromeNumber.java) [![Python](assets/python.png)](python/palindrome_number.py) | |
1819
| 13 | [Roman To Integer](https://leetcode.com/problems/roman-to-integer/) | [![Java](assets/java.png)](src/RomanToInteger.java) [![Python](assets/python.png)](python/roman_to_integer.py) | [![java-yt](assets/java-yt.png)](https://youtu.be/BCue_mO_81A) [![python-yt](assets/python-yt.png)](https://youtu.be/8h_yGTNvKMA) |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/longest-substring-without-repeating-characters
2+
// T: O(|s|)
3+
// S: O(1)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class LongestSubstringWithoutRepeatingCharacters {
9+
public int lengthOfLongestSubstring(String s) {
10+
final Set<Character> letters = new HashSet<>();
11+
int maxLength = 1;
12+
for (int i = 0, j = 0 ; i < s.length() ; i++) {
13+
if (letters.contains(s.charAt(i))) {
14+
while (letters.contains(s.charAt(i))) {
15+
letters.remove(s.charAt(j++));
16+
}
17+
}
18+
letters.add(s.charAt(i));
19+
maxLength = Math.max(maxLength, i - j + 1);
20+
}
21+
return maxLength;
22+
}
23+
}

0 commit comments

Comments
 (0)