Leetcode PHP题解--D73 389. Find the Difference

D73 389. Find the Difference

题目连接

389. Find the Differencephp

题目分析

给定两个字符串,其中一个字符串比另外一个字符串在随机位置多一个字符。spa

返回多出来的字符。.net

思路

用array_count_values计算字符串中字符出现的次数,对比两个字符串的字符出现次数。计算差集,返回差别部分便可。code

最终代码

<?php
class Solution {

    /** * @param String $s * @param String $t * @return String */
    function findTheDifference($s, $t) {
        $ss = array_count_values(str_split($s));
        $tt = array_count_values(str_split($t));
        $diff = array_diff_key($tt, $ss) + array_diff($tt, $ss) + array_diff_assoc($tt, $ss);
        return key($diff);
    }
}
复制代码

若以为本文章对你有用,欢迎用爱发电资助。leetcode

相关文章
相关标签/搜索