389. Find the Difference

旧城等待, 2022-07-15 01:37 260阅读 0赞

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:

  1. Input:
  2. s = "abcd"
  3. t = "abcde"
  4. Output:
  5. e
  6. Explanation:
  7. 'e' is the letter that was added.
  8. public class Solution {
  9. public char findTheDifference(String s, String t) {
  10. char c = 0;
  11. for(int i=0;i<s.length();i++)
  12. c^=s.charAt(i);
  13. for(int i=0;i<t.length();i++)
  14. c^=t.charAt(i);
  15. return c;
  16. }
  17. }

发表评论

表情:
评论列表 (有 0 条评论,260人围观)

还没有评论,来说两句吧...

相关阅读