# 3.longest-substring-without-repeating-characters

## Statement

``````输入: s = "abcabcbb"

``````

``````输入: s = "bbbbb"

``````

``````输入: s = "pwwkew"

请注意，你的答案必须是 子串 的长度，"pwke" 是一个子序列，不是子串。
``````

``````输入: s = ""

``````

• `0 <= s.length <= 5 * 104`
• `s` 由英文字母、数字、符号和空格组成

Given a string `s`, find the length of the longest substring without repeating characters.

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.

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define lowbit(x) ((x) & (-(x)))
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

class Solution {
public:
int lengthOfLongestSubstring(const string &s) {
int len = s.size();
int res = 0;

VI vis(300, 0);

for (int i = 0, j = 0; i < len && j < len; i++) {
while (j < len && vis[s[j]] == 0) {
++vis[s[j]];
++j;
}

chmax(res, j - i);
--vis[s[i]];
}

return res;
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````