Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit series activities – click on the task to see the details of the activities.
A,Topic describes
- Given two strings
s
和t
, they contain only lowercase letters. stringt
By the strings
Randomly rearrange, and then add a letter in a random place. Please find out thet
The letter to be added to. - Example 1:
Enter: s ="abcd", t = "abcde"Output:"e"Explanation:'e'It's the letter that was added.Copy the code
- Example 2:
Enter: s ="", t = "y"Output:"y"
Copy the code
Ii. Analysis of Ideas:
- Find different characters, that is, remove the same characters, and you are left with different characters
- We first convert the given two strings to an array operation
- Iterate through the array and delete the following items when the values are equal
- The last remaining item is the original added character, return
Iii. AC Code:
function findTheDifference(s: string, t: string) :string {
if(s.length === 0) return t;
let sArr = s.split(' ');
let tArr = t.split(' ');
for(let i = 0; i < sArr.length; i++){
for(let j =0 ; j < tArr.length; j++){
if(sArr[i] === tArr[j]){
tArr.splice(j, 1);
break; }}}return tArr[0]};Copy the code
Iv. Summary:
- It’s actually pretty easy, there’s only one way to do it, and there are other ways, like counting and comparing. But the first thought or the above one, there is no trouble. If you want to see other ideas, you can go to the problem solutions and comments section to see other big guy solutions, hee hee hee