Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/longest-substring-without-repeating-characters
Longest Substring Without Repeating Characters
https://github.com/emahtab/longest-substring-without-repeating-characters
hashset leetcode sliding-window
Last synced: about 9 hours ago
JSON representation
Longest Substring Without Repeating Characters
- Host: GitHub
- URL: https://github.com/emahtab/longest-substring-without-repeating-characters
- Owner: eMahtab
- Created: 2020-05-16T00:53:46.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-01-18T01:49:37.000Z (15 days ago)
- Last Synced: 2025-01-18T02:36:31.983Z (15 days ago)
- Topics: hashset, leetcode, sliding-window
- Homepage:
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Longest Substring Without Repeating Characters
## https://leetcode.com/problems/longest-substring-without-repeating-charactersGiven 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.
```
# Implementation 1 : Naive O(n^3)
```java
class Solution {
public int lengthOfLongestSubstring(String s) {
int longest = 0;
for(int i = 0; i < s.length(); i++) {
for(int j = i+1; j < s.length() + 1; j++) {
String substr = s.substring(i, j);
longest = Math.max(longest, check(substr));
}
}
return longest;
}private int check(String str) {
Set set = new HashSet<>();
int length = 0;
for(char ch : str.toCharArray()) {
if(set.contains(ch))
return length;
length++;
set.add(ch);
}
return length;
}
}
```
#### Sliding Window Approach :
1. The idea is to keep moving right pointer and add characters to the hashset as long as we haven't seen it before.2. But if we are at a character `ch` which is already present in the hashset, then it means that current character `ch` is the repeatation of a `previous occurrence` of the same character `ch`, In this case we keep removing elements in the hashset from the left and increment left pointer, we do this until we get rid of the character `ch` from the hashset. By doing this we guarantee that substring between left and right pointer does not contain any duplicate characters. And since we remove the earlier occurrence of `ch` from the hashset, we can count the current `ch` as distinct, so we add it to the hashset.
# Implementation 2 : Sliding Window, Using HashSet, O(n)
```java
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0)
return 0;
int i = 0, j = 0, max = 0;
Set set = new HashSet<>();
while(i < s.length()) {
char ch = s.charAt(i);
while(set.contains(ch)) {
set.remove(s.charAt(j));
j++;
}
set.add(ch);
max = Math.max(max, i-j+1);
i++;
}
return max;
}
}
```
## Implementation 2a : Sliding Window, Using HashMap, O(n)
```java
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length() < 2)
return s.length();Map map = new HashMap<>();
int left = 0;
int right = 0;
int longestSubstringLength = 1;
while(right < s.length()) {
char ch = s.charAt(right);
if(map.containsKey(ch)) {
while(map.containsKey(ch)) {
char charAtLeft = s.charAt(left);
map.remove(charAtLeft);
left++;
}
}
map.put(ch, right);
int substringLength = right - left + 1;
longestSubstringLength = Math.max(longestSubstringLength, substringLength);
right++;
}
return longestSubstringLength;
}
}
```# References :
1. https://leetcode.com/articles/longest-substring-without-repeating-characters
2. https://www.youtube.com/watch?v=4i6-9IzQHwo