<?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 class Node { public $value; public $next = null; // next node public $prev = null; // previous node public function __construct($value) { $this->value = $value; } } class Palindrome { /** •@param string $word •@return bool */ public static function isPalindrome($head, $tail){ if ($head == null) return true; while ($head != $tail){ if ($head->value != $tail->value) return false; $head = $head->next; $tail = $tail->prev; } return true; } } $head = new Node(1); $firstNode = new Node(2); $secondNode = new Node(3); $tail = new Node(4); $head->next = $firstNode; $firstNode->prev = $head; $firstNode->next = $secondNode;...
Comments
Post a Comment