-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindShortestSubstringLength.php
61 lines (46 loc) · 2.12 KB
/
FindShortestSubstringLength.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
class FindShortestSubstringLength
{
public $shortestWindowLength = null;
public $smallestWindowFound = false;
public $searchCharacterHash = [];
public $searchCharacterLength;
public $remainingCharacters;
public $inputString;
public function __construct($searchCharacters)
{
// create a hash for the search characters (unique letters)
for ($i = 0; $i < strlen($searchCharacters); $i++) {
$this->searchCharacterHash[$searchCharacters[$i]] = null;
}
$this->remainingCharacters = count($this->searchCharacterHash);
}
public function find($inputPartial, $startingPosition = 0)
{
for ($i = 0; $i < strlen($inputPartial); $i++) {
$character = $inputPartial[$i];
// if the character is not in the hash, we don't need to keep going
if ( ! array_key_exists($character, $this->searchCharacterHash)) continue;
// otherwise:
// check if there are still remaining characters, that we didn't find yet
if ($this->searchCharacterHash[$character] === null) {
$this->remainingCharacters--;
}
// update current position of the character
$this->searchCharacterHash[$character] = $i + $startingPosition;
if ($this->remainingCharacters > 0) continue;
// get the difference between maximum and minimum position
// to get the window length
$length = max($this->searchCharacterHash) - min($this->searchCharacterHash) + 1;
// now check if the new window length is smaller than before
if ($this->shortestWindowLength === null || $length < $this->shortestWindowLength) {
// update the window
$this->shortestWindowLength = $length;
// and in case we could return early
// check if the smallest window equals the search character hash length
if (count($this->searchCharacterHash) === $length) $this->smallestWindowFound = true;
}
}
return $this->shortestWindowLength;
}
}