Problem Description

Given two strings s and t , they contain only lowercase letters.

The string t is randomly rearranged by the string s and then adds a letter at a random position.

Please find the letters added in t .

Example 1:

 输入:s = "abcd", t = "abcde"
输出:"e"
解释:'e' 是那个被添加的字母。

Example 2:

 输入:s = "", t = "y"
输出:"y"
Leetcode original title address: https://leetcode.cn/problems/find-the-difference

Solution One-turn array comparison and deletion

ideas

Convert the long string t into an array, and traverse the short string s , you can get each item in s , and then put the same item in t删掉( ts多一项, s中有的t Have)

t 28d98a7e3a0128c1338cff2bcc842bab---为'abc's ---9de0470677f7d5774a0361f95ab4c5b1 'ab' ,那么不停对比,不停删除,最后t There is only one left in 'c' , which is not in s t . That is found, the code is as follows:

code

 var findTheDifference = function (s, t) {
    let stack = t.split('') // 1. 把长字符串t转成数组
    for (let j = 0; j < s.length; j++) { // 2. 遍历短字符串s
        let ind = stack.indexOf(s[j]) // 3. 拿到这一项在对应数组中的索引位置
        stack.splice(ind, 1) // 4. 并做对应删除,就这样不停删除,数组中就会只剩一个了
    }
    return stack[0] // 5. 剩的这一个就是t中被添加的字母
};

Solution 2: Count the number of occurrences of words after the t and s strings are combined (find odd numbers)

ideas

  1. First, combine the t and s strings, and then count the number of words that appear in each letter.
  2. It is known that t and s originally look the same, but t has an extra letter.
  3. Assuming that t and s are the same, then after the two strings are merged, the number of occurrences of each letter is 偶数
  4. Now t one more letter, so the number of times a certain letter appears is 奇数
  5. The letter with an odd number of times is the letter added in t

code

 var findTheDifference = function (s, t) {
    let ss = s + t // 1. 合并字符串
    let map = new Map() // 2. 使用集合进行次数统计
    for (let i = 0; i < ss.length; i++) {
        if (map.has(ss[i])) {
            let count = map.get(ss[i])
            count = count + 1
            map.set(ss[i], count)
        } else {
            map.set(ss[i], 1)
        }
    }
    // 3. 统计好以后,遍历这个map结合,看看谁出现的次数是奇数
    for (const [key, value] of map) {
        if (value % 2 == 1) { 
            return key // 4. 次数为奇数的那个字母,就是t中被添加的字母
        }
    }
};

Solution three uses String.charCodeAt() method and String.fromCharCode() method

Knowledge Review

String.charCodeAt() Convert string to Unicode numeric value (UTF-16 code unit)

 console.log( 'a'.charCodeAt() ) // 97
console.log( 'b'.charCodeAt() ) // 98
console.log( 'c'.charCodeAt() ) // 99
console.log( 'd'.charCodeAt() ) // 100
console.log( 'e'.charCodeAt() ) // 101

Since strings can be converted to Unicode values of numeric types, numeric types can also be converted back (if there are any)

String.fromCharCode() Convert the numeric value back to the corresponding string

 console.log( String.fromCharCode(97) ) // 'a'
console.log( String.fromCharCode(98) ) // 'b'
console.log( String.fromCharCode(99) ) // 'c'
console.log( String.fromCharCode(100) ) // 'd'
console.log( String.fromCharCode(101) ) // 'e'

These two APIs are not often used, so sometimes they may not be remembered, but they can play a role here.

ideas

  1. It can be seen from the above that a letter represents a value, t and s are originally equal, but t has an extra string of letters, so.
  2. You can convert all strings in t into numbers and add them together; similarly, convert all strings in s into numbers and add them together
  3. Because t has one more letter than s , so there is one more number when accumulating. So the accumulation and subtraction of the two is the number corresponding to the letter with the more number.
  4. Then use the String.fromCharCode() method to convert the number back to a string

code

 var findTheDifference = function (s, t) {
    // 1. 定义两个变量用于累加
    let countT = 0
    let countS = 0
    for (let i = 0; i < t.length; i++) {
        let item = t[i]
        countT = countT + item.charCodeAt() // 2. 字母转Unicode数字
    }
    for (let j = 0; j < s.length; j++) {
        let item = s[j]
        countS = countS + item.charCodeAt() // 2. 字母转Unicode数字
    }
    // 3. 二者的差值数,转成字符串字母就是多的那个字母
    return String.fromCharCode(countT - countS)
};

水冗水孚
1.1k 声望585 粉丝

每一个不曾起舞的日子,都是对生命的辜负