3. Longest Substring Without Repeating Characters
Problem description:
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a sub-sequence and not a substring.
Solution:
Keep a hashmap
which stores the characters in string as keys and their positions as values, and keep two pointers which define the max substring. move the right pointer to scan through the string , meanwhile update the hashmap. If the character is already in the hashmap
, then move the left pointer to the right of the same character last found. Note that the two pointers can only move forward.
1 | class Solution: |
1 | class Solution { |
time complexity: O(n)
Solution2:
1 | class Solution { |