[389] Find the difference [JavaScript]

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.

Example:

Input:
s = "abcd"
t = "abcde"

Output:
e

Explanation:
'e' is the letter that was added.

Source: LeetCode
Link: https://leetcode-cn.com/problems/find-the-difference

Record a very clever solution to a small simple problem, clear all the elements in t that are the same as s, and the rest are the inserted elements:

/**
 * @param {string} s
 * @param {string} t
 * @return {character}
 */
var findTheDifference = function(s, t) {
    
    
         // 取巧方法, 改变了原数据
  for(let item of s){
    
    
    t = t.replace(item, '')
  }
  return t
  };

Guess you like

Origin blog.csdn.net/weixin_42345596/article/details/106381004