Last active
July 30, 2017 17:25
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
*/ | |
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