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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

The  C Programming Language

The C Programming Language

Brian W. Kernighan、Dennis M. Ritchie / Prentice Hall / 1988-4-1 / USD 67.00

Presents a complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to ta......一起来看看 《The C Programming Language》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具