Longest Substring Without Repeating Characters

原题链接-M

Example
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

时间复杂度: O(N)- 空间复杂度: O(N)

print_r(test("abcabcbb"));

function test($str) {
    if (empty($str)) {
        return 0;
    } 
    
    $strlen = mb_strlen($str);
    $res = $start = $end = 0;
    $lookup = [];
    
   while ($start < $strlen && $end < $strlen) {
       $nowStr = $str[$end];
       if (in_array($nowStr,$lookup)) {
           $deleStr = substr($str,0,$start);
           $lookup = array_diff($lookup,[$deleStr]);
           $start ++;
       } else {
            $lookup[] = $nowStr;
            $res = max($res, $end-$start+1);
            $end ++;
       }
   }
   return $res;
}

你可能感兴趣的:(Longest Substring Without Repeating Characters)