Last active
March 7, 2017 06:52
-
-
Save ravinsharma12345/b5c3ef1fe99fc309b22e49ab932e136c to your computer and use it in GitHub Desktop.
Test a string is isogram of any order/Get order of isogram
This file contains 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
function find_isogram_order($word) { | |
if(empty($word)) | |
return; | |
foreach(str_split(strtolower($word)) as $char) { | |
if(is_numeric($char)) { | |
return; | |
} | |
if(!isset($sequence[$char])) { | |
$sequence[$char] = 1; | |
} | |
else { | |
$sequence[$char]++; | |
} | |
} | |
if(count(array_flip($sequence)) === 1) { | |
return current($sequence); | |
} | |
} | |
$values = [ | |
"", | |
"1111", | |
"1111ssss", | |
"Dermatoglyphics", | |
"aba", | |
"moOse", | |
"booBsB", | |
"booB", | |
"Caucasus", | |
"geggee", | |
"vexes" | |
]; | |
foreach($values as $value) { | |
if($order = find_isogram_order($value) and is_numeric($order)) { | |
echo "$value, order: $order".PHP_EOL; | |
} | |
} |
This file contains 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
// repl https://repl.it/GKMW/4 | |
function find_isogram_order($word) { | |
if(empty($word)) | |
return; | |
foreach(str_split(strtolower(str_replace(' ', '', $word))) as $char) { | |
if(is_numeric($char)) { | |
return; | |
} | |
if(!isset($sequence[$char])) { | |
$sequence[$char] = 1; | |
} | |
else { | |
$sequence[$char]++; | |
} | |
} | |
if(count(array_flip($sequence)) === 1) { | |
return current($sequence); | |
} | |
} | |
function find_wordset_isogram_order(array $wordset = array()) { | |
$orders = []; | |
foreach($wordset as $word) { | |
$order = find_isogram_order($word); | |
if($order > 0) { | |
$orders[$word] = $order; | |
} | |
else { | |
$order[$word] = 0; | |
} | |
} | |
return $orders; | |
} | |
$wordset = [ | |
'myth duck', | |
'myth duck verb', | |
'myth duck verb gasp', | |
'myth duck verb gasp jinx', | |
'myth duck verb gasp jinx wolf', | |
'abdest', | |
'abdest chi', | |
'abdest chi group', | |
"", | |
"1111", | |
"1111ssss", | |
"Dermatoglyphics", | |
"aba", | |
"moOse", | |
"booBsB", | |
"booB", | |
"Caucasus", | |
"geggee", | |
"vexes" | |
]; | |
var_dump(find_wordset_isogram_order($wordset)); |
This file contains 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
function is_isogram($word) { | |
if(empty($word)) | |
return false; | |
foreach(str_split(strtolower($word)) as $char) { | |
if(is_numeric($char)) { | |
return false; | |
} | |
if(!isset($sequence[$char])) { | |
$sequence[$char] = 1; | |
} | |
else { | |
$sequence[$char]++; | |
} | |
} | |
if(count(array_flip($sequence)) === 1) { | |
return true; | |
} | |
return false; | |
} | |
$values = [ | |
"", | |
"1111", | |
"1111ssss", | |
"Dermatoglyphics", | |
"aba", | |
"moOse", | |
"booBsB", | |
"booB", | |
"Caucasus", | |
"geggee", | |
"vexes" | |
]; | |
foreach($values as $value) { | |
if(is_isogram($value)) { | |
echo $value.PHP_EOL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment