当前位置:   article > 正文

389. Find the Difference(Leetcode每日一题-2020.12.18)_you are given two string s and t

you are given two string s and t

Problem

You are given two strings s and t.

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

Return the letter that was added to t.

Constraints:

  • 0 <= s.length <= 1000
  • t.length == s.length + 1
  • s and t consist of lower-case English letters.

Example1

Input: s = “abcd”, t = “abcde”
Output: “e”
Explanation: ‘e’ is the letter that was added.

Example2

Input: s = “”, t = “y”
Output: “y”

Example3

Input: s = “a”, t = “aa”
Output: “a”

Example4

Input: s = “ae”, t = “aea”
Output: “a”

Solution

class Solution {
public:
    char findTheDifference(string s, string t) {
        int x = 0;
        for (auto c: s) x ^= c;
        for (auto c: t) x ^= c;
        return x;
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/71597
推荐阅读
相关标签
  

闽ICP备14008679号