Leetcode PHP题解--D102 383. Ransom Note
D102 383. Ransom Note
题目链接
题目分析
给定一个字符串,判断这些字母在另一个给定的字符串中,出现次数是否相等。
思路
先获取两个字符串中各字符的出现次数,再逐个遍历,判断出现次数是否满足条件。
最终代码
<?php class Solution { /** * @param String $ransomNote * @param String $magazine * @return Boolean */ function canConstruct($ransomNote, $magazine) { if(!$ransomNote){ return true; } $rArr = array_count_values(str_split($ransomNote)); $mArr = array_count_values(str_split($magazine)); foreach($rArr as $char => $amount){ if(!isset($mArr[$char])){ return false; } if($mArr[$char]<$amount){ return false; } } return true; } }
若觉得本文章对你有用,欢迎用[爱发电](https://afdian.net/@skys215)资助。
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23