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

栏目: PHP · 发布时间: 7年前

内容简介:给定两个字符串,其中一个字符串比另一个字符串在随机位置多一个字符。返回多出来的字符。

D73 389. Find the Difference

题目链接

389. Find the Difference

题目分析

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

返回多出来的字符。

思路

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

最终代码

<?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);
                                                                }
                                                                }
若觉得本文章对你有用,欢迎用[爱发电](https://afdian.net/@skys215)资助。

以上所述就是小编给大家介绍的《Leetcode PHP题解--D73 389. Find the Difference》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

How to Solve It

How to Solve It

Zbigniew Michalewicz、David B. Fogel / Springer / 2004-03-01 / USD 59.95

This book is the only source that provides comprehensive, current, and detailed information on problem solving using modern heuristics. It covers classic methods of optimization, including dynamic pro......一起来看看 《How to Solve It》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具