Given 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.
Thinking:O(n)
#0. we need to substring [i,j) and then mark down Start position of the cursor and its End by hashmap;
#2. in the meantime, we need to calculate the length of substring.
#3. if character exists already, we need to move the start position of cursor to current position.
#4. check if the new length of substring is longer than origin one . yes. please replace it .
Note: remember plus 1 in the map value.