Given two strings s and t, they only contain lowercase letters. The string t is randomly rearranged by the string s, and then a letter is added at a random position. Please find the letter added in t

Title: Find the difference.
Given two strings s and t, they only contain lowercase letters.
The string t is randomly rearranged by the string s, and then a letter is added at a random position.
Please find the letter added in t.
Send the original title
Insert picture description here

Tip:
0 <= s.length <= 1000
t.length == s.length + 1
s and t only contain lowercase letters

Problem-solving idea:
Analyzing the problem, we found that there is only one different character in the string, so we can use the feature of bitwise operation (exclusive OR) to solve the problem. The code is as follows:

class Solution {
    
    
public:
	char findTheDifference(string s, string t) {
    
    
		char res = 0;//类似于 力扣 136 :只出现一次的数字
		string str = s + t;
		for (auto cha : str)
			res ^= cha;
		return res;
	}
};

Guess you like

Origin blog.csdn.net/Genius_bin/article/details/113740324