Leetcode 389. Find the Difference 找不同 解题报告

1 解题思想

有两个字符串S和T,T是S打乱后多加一个字符,要找出这个不同的字符来。

嗯,这个问题其实就是异或,除了T多的那个字符外,都可以在异或中消除,不多说,很简单

2 原题

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:
e

Explanation:
'e' is the letter that was added.

3 AC解

public class Solution {
    public char findTheDifference(String s, String t) {
        char tmp = 0x00;
        for(int i=0;ichar)( tmp ^ s.charAt(i));
        for(int i=0;ichar)(tmp ^ t.charAt(i));
        return tmp;
    }
}

你可能感兴趣的:(leetcode-java)