I want to write code that finds the maximum sequence of characters at the end of an array that are the same as the beginning of the array.
But I dont know how I can do it with PHP?
For example:
Input = [a,b,c,e,r,t,x,s,b,a,b,c]
Output = [a,b,c]
(because the elements a,b,c are both at the beginning and end of the array and they represent the maximum sequence of such characters)
Note: This will work perfectly for this kind of array, where we have array of strings, it does not work for nested array.
<?php
ini_set('display_errors', 1);
$data = array("a","b","c","e","r","t","x","s","b","a","b","c");
$string= implode("", $data);//converting array to string.
for($x=strlen($string)-1;$x>=0;$x--)
{
//matching substring from the end of string.
if(preg_match("/".substr($string, 0,$x)."$/",$string)==true)
{
$string= substr($string, 0,$x);
break;
}
}
$result=str_split($string);
print_r($result);
I hope this code will work:
<?php
$Input = array('a','b','c','e','r','t','x','s','b','a','b','c');
$len=count($Input);
$j=$len-1;
$count=0;
$s=0;
$k=$n=0;
$a[$len/2];
for($i=0;$i<$len;$i++)
{
if($Input[$i]!=$Input[$j]){
$j--;
$i--;
}
if($Input[$i]==$Input[$j]){
$count++;
$a[$n]=$Input[$j];
$n++;
if($k==$j)
{
$s++;
break;
}
$k=$j;
if($j!=$len-1)
$j++;
else
break;
}
}
if($s!=0)
echo "sequence not present";
else
{
echo "<br>sequence present <br>";
$len2=count($a);
for($p=0;$p<$len2;$p++)
echo" ".$a[$p];
}
?>
sunny, I've got a good one for you!
Method:
$found=false; // declare default outcome
for($x=1,$max=sizeof($data); $x<=$max; ++$x){ // this allows "overlap"
if(array_slice($data,0,$x)===array_slice($data,-$x)){ // compare start to end
$found=true; // declare a match has occurred
}elseif($found){ // this iteration is no match
--$x; // rewind to successful match
break;
}
}
var_export($found?array_slice($data,0,$x):"No match"); // output the result
Inputs & Outputs:
$data=['a','b','c','e','r','t','x','s','b','a','b','c']; // ['a','b','c']
$data=['n','o','p','e']; // No Match
$data=['r','a','c','e','c','a','r']; // ['r']
$data=['a','a','b','a','a']; // ['a','a']
Explanation:
It is more efficient and advisable to avoid regex based solutions whenever possible. Furthermore, I managed to write a solution that keeps the input in array form (avoiding unnecessary conversions).
array_slice() is the clear hero of this answer. As $x increments, the two array_slice() calls stay in sync allowing a simple conditional comparison.
$max is set to iterate the whole array and welcomes the possibility of "overlap" within the array. If you don't want any chance of "overlap", you can use $max=floor(sizeof($data)/2)
After a match is found, as soon as there is a non-match, the loop will break and the correct output will be displayed.
Question extension...
Palindromic Matching -- You can easily adjust my above method to match mirrored sequences by adding array_reverse().
Method:
$found=false;
for($x=1,$max=sizeof($data); $x<=$max; ++$x){
if(array_slice($data,0,$x)===array_reverse(array_slice($data,-$x))){ // only change
$found=true;
}elseif($found){
--$x;
break;
}
}
var_export($found?array_slice($data,0,$x):"No match");
Inputs & Outputs:
$data=['a','b','c','e','r','t','x','s','b','a','b','c']; // No Match
$data=['n','o','p','e']; // No Match
$data=['r','a','c','e','c','a','r']; // ['r','a','c','e','c','a','r']
$data=['a','a','b','a','a']; // ['a','a','b','a','a']