2022-07-14 09:40:42 +08:00
|
|
|
# Leetcode Longest-Substring-Without-Repeating-Characters
|
|
|
|
|
|
|
|
#### 2022-07-14 09:33
|
|
|
|
|
|
|
|
> ##### Algorithms:
|
2022-07-14 10:36:52 +08:00
|
|
|
> #algorithm #Kadane_s_algorithm #sliding_window
|
2022-07-14 09:40:42 +08:00
|
|
|
> ##### Data structures:
|
|
|
|
> #DS #string
|
|
|
|
> ##### Difficulty:
|
|
|
|
> #coding_problem #difficulty-medium
|
|
|
|
> ##### Additional tags:
|
|
|
|
> #leetcode
|
|
|
|
> ##### Revisions:
|
|
|
|
> N/A
|
|
|
|
|
|
|
|
##### Related topics:
|
|
|
|
```expander
|
2022-07-14 10:36:52 +08:00
|
|
|
tag:#Kadane_s_algorithm OR tag:#sliding_window
|
2022-07-14 09:40:42 +08:00
|
|
|
```
|
2022-07-14 10:02:54 +08:00
|
|
|
|
|
|
|
|
2022-07-14 09:40:42 +08:00
|
|
|
##### Links:
|
|
|
|
- [Link to problem](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
|
|
|
|
___
|
|
|
|
### Problem
|
|
|
|
|
|
|
|
Given a string `s`, find the length of the **longest substring** without repeating characters.
|
|
|
|
|
|
|
|
#### Examples
|
|
|
|
|
|
|
|
**Example 1:**
|
|
|
|
|
|
|
|
**Input:** s = "abcabcbb"
|
|
|
|
**Output:** 3
|
|
|
|
**Explanation:** The answer is "abc", with the length of 3.
|
|
|
|
|
|
|
|
**Example 2:**
|
|
|
|
|
|
|
|
**Input:** s = "bbbbb"
|
|
|
|
**Output:** 1
|
|
|
|
**Explanation:** The answer is "b", with the length of 1.
|
|
|
|
|
|
|
|
**Example 3:**
|
|
|
|
|
|
|
|
**Input:** s = "pwwkew"
|
|
|
|
**Output:** 3
|
|
|
|
**Explanation:** The answer is "wke", with the length of 3.
|
|
|
|
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
|
|
|
|
|
|
|
#### Constraints
|
|
|
|
|
|
|
|
- `0 <= s.length <= 5 * 104`
|
|
|
|
- `s` consists of English letters, digits, symbols and spaces.
|
|
|
|
|
|
|
|
### Thoughts
|
|
|
|
|
|
|
|
> [!summary]
|
2022-07-14 10:02:54 +08:00
|
|
|
> This is a #Kadane_s_algorithm
|
|
|
|
|
|
|
|
Initially, I thought of a kadane's algo, but implemented wrong.
|
|
|
|
|
|
|
|
Then I figured out kadane's algorithm.
|
2022-07-14 09:40:42 +08:00
|
|
|
|
2022-07-14 10:02:54 +08:00
|
|
|
similar to this one.
|
|
|
|
[[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
|
|
|
|
|
|
|
|
The goal is making cur as small as possible, without duplicating
|
2022-07-14 09:40:42 +08:00
|
|
|
|
2022-07-14 10:09:31 +08:00
|
|
|
And the localMax is max(localMax, i - cur)
|
|
|
|
|
2022-07-14 09:40:42 +08:00
|
|
|
### Solution
|
|
|
|
|
2022-07-14 10:02:54 +08:00
|
|
|
Kadane's algorithm
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
class Solution {
|
|
|
|
public:
|
|
|
|
int lengthOfLongestSubstring(string s) {
|
|
|
|
// Kadane's algorithm
|
|
|
|
vector<int> hMap(255, -1);
|
|
|
|
|
|
|
|
int cur = -1;
|
|
|
|
int localMax = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < s.length(); i++) {
|
|
|
|
if (hMap[s[i]] > cur) {
|
|
|
|
// If the element occurs again, reset
|
|
|
|
cur = hMap[s[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
hMap[s[i]] = i;
|
2022-07-14 10:09:31 +08:00
|
|
|
// The char at cur is ignored
|
2022-07-14 10:02:54 +08:00
|
|
|
localMax = max(localMax, i - cur);
|
|
|
|
}
|
|
|
|
return localMax;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2022-07-14 09:40:42 +08:00
|
|
|
Initial solution
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
class Solution {
|
|
|
|
public:
|
|
|
|
int lengthOfLongestSubstring(string s) {
|
|
|
|
unordered_map<char, int> hMap;
|
|
|
|
int cur = 0;
|
|
|
|
int localMax = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < s.size(); i++) {
|
|
|
|
if (hMap.find(s[i]) == hMap.end()) {
|
|
|
|
cur++;
|
|
|
|
localMax = max(cur, localMax);
|
|
|
|
hMap[s[i]] = i;
|
|
|
|
} else {
|
|
|
|
i = hMap[s[i]];
|
|
|
|
hMap.clear();
|
|
|
|
cur = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return localMax;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
```
|