Skip to content

Commit fd3f6b4

Browse files
committed
Solved problem 3 : Longest Substring Without Repeating Characters
1 parent 5654aec commit fd3f6b4

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### Problems Solved
1818

19-
| Total | 50 |
19+
| Total | 51 |
2020
|:---:|:---:|
2121

2222
#### Search By Topic
@@ -37,7 +37,7 @@
3737
| Linked Lists | 5 |
3838
| Math & Geometry | 4 |
3939
| Priority Queue | 3 |
40-
| Sliding Window | 1 |
40+
| Sliding Window | 2 |
4141
| Stack | 2 |
4242
| Tries | 0 |
4343
| Two Pointers | 4 |
@@ -47,7 +47,7 @@
4747
| Difficulty | Number |
4848
|:---|---:|
4949
| Easy | 45 |
50-
| Medium | 5 |
50+
| Medium | 6 |
5151
| Hard | 0 |
5252

5353
## Milestones
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Problem: 3
3+
* Name: Longest Substring Without Repeating Characters
4+
* Difficulty: Medium
5+
* Topic: Sliding Window
6+
* Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
7+
*/
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
// Unordered Set
13+
// Time Complexity: O(n)
14+
// Space Complexity: O(n)
15+
int lengthOfLongestSubstringUS(string s) {
16+
int result = 0;
17+
int left = 0, right = 0;
18+
unordered_set<char> chars;
19+
while (right < s.size()){
20+
while (chars.count(s[right]) > 0) {
21+
chars.erase(s[left]);
22+
left++;
23+
}
24+
chars.insert(s[right]);
25+
result = max(result, right - left + 1);
26+
right++;
27+
}
28+
return result;
29+
}
30+
31+
// Bool Vector
32+
// Time Complexity: O(n)
33+
// Space Complexity: O(256)
34+
int lengthOfLongestSubstringV(string s) {
35+
int result = 0;
36+
int left = 0, right = 0;
37+
vector<bool> chars(256,0);
38+
while (right < s.size()){
39+
while (chars[s[right]]) {
40+
chars[s[left]] = 0;
41+
left++;
42+
}
43+
chars[s[right]] = 1;
44+
result = max(result, right - left + 1);
45+
right++;
46+
}
47+
return result;
48+
}

0 commit comments

Comments
 (0)