389. Find the Difference

来源:互联网 发布:最好的win10优化工具 编辑:程序博客网 时间:2024/06/14 21:36

问题: Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:Input:s = "abcd"t = "abcde"Output:eExplanation:'e' is the letter that was added.

解题思路:在t数组中查找有一样的字母,将它改为大写,最后剩下的小写字母就是要查找的字符。
解题思路:用长度为26的数组count来记录字母相对应的出现次数,s数组字符就在相应的位置加一,t数组字符就在相应的位置减一,最后count不为0的位置转换成字符就是所要查找的字符。
解题思路:用一个变量ans来统计数组s的异或结果,再与t数组来异或,出现两次的字符就会为零,最后ans剩下的就是另外添加的字符。

原创粉丝点击