猜字谜
解法
首先将word单词进行二进制压缩,重复的单词去掉,组成sword哈希映射。然后循环puzzle,先求出puzzle的所有解(这个可以用leetcode每日一题之4.子集来求),然后在sword中匹配,加上相应的数量。代码如下
class Solution {
/**
* @param String[] $words
* @param String[] $puzzles
* @return Integer[]
*/
function findNumOfValidWords($words, $puzzles) {
// 首先构造相关的数据结构
$swords = [];
foreach($words as $word){
$length = strlen($word);
$wordValue = 0;
for($i = 0; $i < $length; $i++){
$wordValue |= (1 << ord($word[$i]) - ord('a'));
}
if ($this->countOne($wordValue) <= 7){
$swords[$wordValue] = isset($swords[$wordValue]) ? $swords[$wordValue] + 1 : 1;
}
}
$spuzzle = [];
foreach($puzzles as $puzzle){
$total = 0;
$test = (1 << ord($puzzle[0]) - ord('a'));
if(array_key_exists($test, $swords)) $total += $swords[$test];
// 求出谜面所有的解
$puzzleValue = 0;
$length = strlen($puzzle);
$tmps = [$test];
for($i = 1; $i < $length; $i++){
foreach($tmps as $tmp){
$test = $tmp | (1 << ord($puzzle[$i]) - ord('a'));
if(array_key_exists($test, $swords)) $total += $swords[$test];
$tmps[] = $test;
}
}
$spuzzle[] = $total;
}
return $spuzzle;
}
function countOne($n){
$string = decbin($n);
$number = 0;
$count = strlen($string);
for($i = 0; $i < $count; $i++){
if($string[$i] == 1) $number++;
}
return $number;
}
}