-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum-window-substring.php
More file actions
117 lines (100 loc) · 2.92 KB
/
Copy pathminimum-window-substring.php
File metadata and controls
117 lines (100 loc) · 2.92 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
|--------------------------------------------------------------------------
| Minimum Window Substring
|--------------------------------------------------------------------------
| #76
| Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every
| character in t (including duplicates) is included in the window. If there is no such substring, return the empty
| string "".
|
| The testcases will be generated such that the answer is unique.
|
| @link https://leetcode.com/problems/minimum-window-substring/
|
*/
class Solution {
/**
* @param string $s
* @param string $t
* @return string
*/
public function minWindow(string $s, string $t) : string
{
// Haystack len, needle lens.
$haystack = '';
$m = strlen($s);
$n = strlen($t);
// constraints
if ($m < 1 || $n > pow(10, 5)) {
return '';
}
if ($n > $m) {
return $haystack;
}
// Return minimum substring of $s that has at least all the chars of $t any additional chars are acceptable as
// long as they're continuous.
$offset = 0;
$haystackLen = $n;
do {
$haystack = substr($s, $offset, $haystackLen);
// Increment haystack len after exhausting options.
$chkHaystackLen = ($m - $haystackLen);
if ($offset === $chkHaystackLen) {
$offset = 0;
$haystackLen++;
} else {
$offset++;
}
} while (!$this->chkHaystack($haystack, $t, $n));
return $haystack;
}
/**
* Check if str (haystack) has all chars of t.
*
* @param string $haystack
* @param string $t
* @param int $n
* @return bool
*/
protected function chkHaystack(string $haystack, string $t, int $n): bool
{
for ($k = 0; $k < $n; $k++) {
$needle = $t[$k];
if (!str_contains($haystack, $needle)) {
return false;
}
if (!$this->cmpNeedles($haystack, $needle, $t)) {
return false;
}
}
// Now that we have all needles found in haystack check if the len is greater or equal to the len of $t.
return strlen($haystack) >= $n;
}
/**
* Does the count === count in t
*
* @param string $haystack
* @param string $needle
* @param string $t
* @return bool
*/
protected function cmpNeedles(string $haystack, string $needle, string $t): bool
{
return substr_count($t, $needle) <= substr_count($haystack, $needle);
}
}
// Sample Inputs
$s = 'ADOBECODEBANC';
$t = 'ABC';
$ans = 'BANC';
$obj = new Solution();
$output = $obj->minWindow($s, $t);
print_r([
's' => $s,
't' => $t,
'output' => $output
]);
if ($output === $ans) {
echo "Your output ($output) is correct!\n";
}