# Find Difference

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.
```

## Note

看成整体，加加减减，用异或或者一个变量都行

## Code

```java
public char findTheDifference(String s, String t) {
    // Convert to arrays for easy access
    char[] sChars = s.toCharArray();
    char[] tChars = t.toCharArray();

    // Initially, char code is the last t character, since that won't be included in the loop
    int charCode = tChars[tChars.length - 1];

    // For all positions (except the "bonus" one handled above), change running char total
    for (int i = 0; i < s.length(); i++) {
          charCode -= (int)sChars[i];
          charCode += (int)tChars[i]; 
    }

    // Leftover amount is the value of bonus letter
    return (char)charCode;
}
```

```java
class Solution {
    public char findTheDifference(String s, String t) {
        char res = t.charAt(t.length() - 1);
        for (int i = 0; i < s.length(); i++) {
            res ^= t.charAt(i);
            res ^= s.charAt(i);
        }
        return res;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://luj.gitbook.io/code/tricky/find-difference.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
