Add PHP example for chapter 09 - dynamic programming (#98)
This commit is contained in:
committed by
Aditya Bhargava
parent
cc845c40f7
commit
bac32b613b
45
09_dynamic_programming/php/01_longest_common_subsequence.php
Normal file
45
09_dynamic_programming/php/01_longest_common_subsequence.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @param string $stringA
|
||||
* @param string $stringB
|
||||
* @return array
|
||||
*/
|
||||
function search(string $stringA, string $stringB): array
|
||||
{
|
||||
$cell = [];
|
||||
|
||||
for ($i = 0; $i < strlen($stringA); $i++) {
|
||||
for ($j = 0; $j < strlen($stringB); $j++) {
|
||||
if ($stringA[$i] === $stringB[$j]) {
|
||||
if (isset($cell[$i - 1][$j - 1])) {
|
||||
$cell[$i][$j] = $cell[$i - 1][$j - 1] + 1;
|
||||
} else {
|
||||
$cell[$i][$j] = 1;
|
||||
}
|
||||
} else {
|
||||
if (isset($cell[$i - 1][$j]) || isset($cell[$i][$j - 1])) {
|
||||
$cell[$i][$j] = max(
|
||||
isset($cell[$i - 1][$j]) ? $cell[$i - 1][$j] : 0,
|
||||
isset($cell[$i][$j - 1]) ? $cell[$i][$j - 1] : 0
|
||||
);
|
||||
} else {
|
||||
$cell[$i][$j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $cell;
|
||||
}
|
||||
|
||||
print_r(search('fish', 'fosh'));
|
||||
|
||||
/*
|
||||
[ f o s h
|
||||
f [1,1,1,1],
|
||||
i [1,1,1,1],
|
||||
s [1,1,2,2],
|
||||
h [1,1,2,3]
|
||||
]
|
||||
*/
|
||||
Reference in New Issue
Block a user