Longest Substring Without Repeat
PHP
Hard
5 views
Problem Description
Find the length of the longest substring with all unique characters.
Input Format
One line string s.
Output Format
One integer length.
Official Solution
<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
$last=[];
$start=0; $best=0;
for($i=0,$n=strlen($inputText);$i<$n;$i++){
$ch=$inputText[$i];
if(isset($last[$ch]) && $last[$ch] >= $start) $start=$last[$ch]+1;
$last[$ch]=$i;
$best=max($best,$i-$start+1);
}
echo $best;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!