Skip to content

Instantly share code, notes, and snippets.

@bunlongheng
Last active Mar 1, 2019
Embed
What would you like to do?
10/1/2015
function containsKey($key_para, $array)
{
foreach ($array as $key => $value) {
if ($key_para === $key) {
return true;
}
}
return false;
}
function findRank($perm)
{
$a = array();
$rank = 1;
$suffix = 1;
$length = strlen($perm);
$characters = str_split($perm);
for ($i = $length - 1; $i > -1; $i--) {
$char = $characters[$i];
$count = 1;
if ($this->containsKey($char, $a)) {
$count = $a[$char];
$count++;
$a[$char] = $count;
} else {
$a[$char] = $count;
}
foreach ($a as $key => $value) {
if (ord($key) < ord($characters[$i])) {
$rank = $rank + (($suffix * $value) / $count);
}
}
$suffix = $suffix * ($length - $i);
$suffix = ($suffix / $count);
}
return $rank;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment