Skip to content

Instantly share code, notes, and snippets.

@HoangPV
Last active July 30, 2017 17:25
Show Gist options
  • Save HoangPV/482ecef45b868fa8fea144eb015eb9c1 to your computer and use it in GitHub Desktop.
Save HoangPV/482ecef45b868fa8fea144eb015eb9c1 to your computer and use it in GitHub Desktop.
return a boolean true if all rotations of strng are included in arr (C returns 1
<?php
/**
* return a boolean true if all rotations of strng are included in arr (C returns 1)
*
* @param string $s
* @param array $arr
* @return boolean
* @author Phan Vu Hoang <vu-hoang.phan@ekino.com>
*/
function containAllRots($s, $arr)
{
// all rotations
$s2 = array();
for ($i = 0; $i < strlen($s); $i++) {
$s2[] = substr($s, $i) . substr($s, 0, $i);
}
return (array_intersect($s2, $arr) === $s2) ? true : false;
}
function containAllRots_alt($s, $arr) {
foreach (str_split($s) as $char) {
$s = substr($s, 1) . $char;
if(!in_array($s, $arr)) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment