<?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;
$secondNode->prev = $firstNode;
$secondNode->next = $tail;
echo Palindrome::isPalindrome($head, $tail);
<?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 ...
Comments
Post a Comment