<?php
Class LRS{
/**
•@param array $texts
•Prints longest repeated substrings for each text */
public static function getAllLRS($texts){
$stringArr = array();
foreach($texts as $string){
$stringArr[] = self::LongestRepeatedSubstring($string);
}
return $stringArr;
}
public function LongestRepeatedSubstring($string){
if ($string == null)
return null;
$string_length = strlen($string);
$substrings = array();
for ($i=0; $i < $string_length; $i++){
$substrings[$i] = substr($string, $i);
}
sort($substrings);
$result = "";
for ($i = 0; $i < $string_length - 1; $i++){
$lcs = self::LongestCommonString($substrings[$i], $substrings[$i + 1]);
if (strlen($lcs) > strlen($result)){
$result = $lcs;
}
}
return $result;
}
function LongestCommonString($a, $b){
$n = min(strlen($a), strlen($b));
$lcs_result = "";
for ($i = 0; $i < $n; $i++){
if ($a[$i] == $b[$i]){
$lcs_result = $lcs_result.$a[$i];
}else{
break;
}
}
return $lcs_result;
}
}
print_r(LRS::getAllLRS(['ABCDEFG','banana','abcpqrabpqpq']));
?>
<?php interface LRUCache{ /** •@param string $key •@param string $value •@return bool $result • •Stores value against the key in the cache */ public function insertIntoCache($key,$value); /** •@param string $key •@return string $value •Gets the value of a key from the cache */ public function getFromCache($key); /** Purge the entire cache */ public function purgeCache(); /** •@return int $count •Gets the number of successful cache hits so far */ public function allCacheHits(); /** •@return int $count •Gets the number of unsuccessful cache hits so far **/ public function allCacheMissed(); } class Cache implements LRUCache{ // int the max number of elements the cache supports private $capacity; // Array representing a naive hashmap (TODO needs to pass t
Comments
Post a Comment