389. Find the Difference
Problem Description
You are given two strings s and t.
String t is generated by random shuffling string s and then adding one more letter at a random position.
Return the letter that was added to t.
Example 1:
Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.
Example 2:
Input: s = "", t = "y"
Output: "y"
Constraints:
0 <= s.length <= 1000t.length == s.length + 1sandtconsist of lowercase English letters.
Solution
Python Solution
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
# XOR approach
result = 0
# XOR all characters in s
for char in s:
result ^= ord(char)
# XOR all characters in t
for char in t:
result ^= ord(char)
# The remaining value is the added character
return chr(result)
Time Complexity: O(n)
Where n is the length of string t. We process each character once.
Space Complexity: O(1)
We only use a single variable to store the XOR result.
Java Solution
class Solution {
public char findTheDifference(String s, String t) {
// XOR approach
int result = 0;
// XOR all characters in s
for (char c : s.toCharArray()) {
result ^= c;
}
// XOR all characters in t
for (char c : t.toCharArray()) {
result ^= c;
}
// The remaining value is the added character
return (char) result;
}
}
Time Complexity: O(n)
Where n is the length of string t. We process each character once.
Space Complexity: O(1)
We only use a single variable to store the XOR result.
C++ Solution
class Solution {
public:
char findTheDifference(string s, string t) {
// XOR approach
char result = 0;
// XOR all characters in s
for (char c : s) {
result ^= c;
}
// XOR all characters in t
for (char c : t) {
result ^= c;
}
// The remaining value is the added character
return result;
}
};
Time Complexity: O(n)
Where n is the length of string t. We process each character once.
Space Complexity: O(1)
We only use a single variable to store the XOR result.
JavaScript Solution
/**
* @param {string} s
* @param {string} t
* @return {character}
*/
var findTheDifference = function(s, t) {
// XOR approach
let result = 0;
// XOR all characters in s
for (let char of s) {
result ^= char.charCodeAt(0);
}
// XOR all characters in t
for (let char of t) {
result ^= char.charCodeAt(0);
}
// The remaining value is the added character
return String.fromCharCode(result);
};
Time Complexity: O(n)
Where n is the length of string t. We process each character once.
Space Complexity: O(1)
We only use a single variable to store the XOR result.
C# Solution
public class Solution {
public char FindTheDifference(string s, string t) {
// XOR approach
char result = (char)0;
// XOR all characters in s
foreach (char c in s) {
result ^= c;
}
// XOR all characters in t
foreach (char c in t) {
result ^= c;
}
// The remaining value is the added character
return result;
}
}
Time Complexity: O(n)
Where n is the length of string t. We process each character once.
Space Complexity: O(1)
We only use a single variable to store the XOR result.
Approach Explanation
The solution uses the XOR operation to find the added character:
- Key Insights:
- XOR properties
- Character manipulation
- Constant space
- Linear time
- Algorithm Steps:
- Initialize result
- XOR all chars in s
- XOR all chars in t
- Convert result
Implementation Details:
- XOR operation
- Character conversion
- String traversal
- Result handling
Optimization Insights:
- No extra space
- Single pass
- Bit manipulation
- Direct result
Edge Cases:
- Empty string s
- Single character t
- Same characters
- Different positions