Sub Anagram

function subAnagram($string, $target) {
  $string = str_split($string);
  $target = str_split($target);
   
  $tempTarget = $target;
  $removed = [];
  
  $start = 0;
  $end = 0;
  $position = 0;

  foreach ($string as $char) {

    if (array_search($char, $tempTarget) === false) {

      $key = array_search($char, $removed);
      if ($key !== false) {
        $tempTarget[] = $removed[$key];
        unset($removed[$key]);
        $start++;
      }
      else {
        $tempTarget = $target;
        $removed = [];
        $start = $position + 1;
      }
    }
    
    $key = array_search($char, $tempTarget);
    if ($key !== false) {
      $removed[] = $tempTarget[$key];
      unset($tempTarget[$key]);
      if(count($tempTarget)==0) {
        $end = $position;
        print "$start, $end\n";
        return true;
      }
    }
    $position++;
  }
}

$string = 'vvaaamarioklkk';
$target = 'aaama';
subAnagram($string,$target);   // output: 1
    
index