fork download
  1. <?php
  2. $str = 'abcccdefghijklm234nnnnopqqrerrstuvwxyz0156789';
  3.  
  4. $cur = [
  5. 'L' => 0,
  6. 'R' => 0,
  7. ];
  8.  
  9. $max = [
  10. 'L' => 0,
  11. 'R' => 0,
  12. ];
  13.  
  14. $i = 0;
  15. $chars = [];
  16.  
  17. do {
  18. $char = substr( $str, $cur['R'], 1);
  19. $pos = array_search( $char, $chars);
  20.  
  21. if( false === $pos) {
  22. array_push( $chars, $char);
  23. $cur['R'] = $cur['L'] + count( $chars) - 1;
  24. } else {
  25. if( $max['R'] - $max['L'] < $cur['R'] - $cur['L']) {
  26. $max = $cur;
  27. $max['R']--;
  28. }
  29.  
  30. array_push( $chars, $char);
  31. array_splice( $chars, 0, $pos+1);
  32. $cur['L'] += $pos + 1;
  33. }
  34.  
  35. } while( ++$cur['R'] < strlen($str));
  36.  
  37. if( $max['R'] - $max['L'] < $cur['R'] - $cur['L']) {
  38. $max = $cur;
  39. $max['R']--;
  40. }
  41.  
  42. print_r($max);
  43. print( substr( $str, $max['L'], $max['R'] - $max['L'] + 1)) . PHP_EOL;
  44.  
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
Array
(
    [L] => 29
    [R] => 44
)
rstuvwxyz0156789