The title

Title link: leetcode-cn.com/leetbook/re…



Answer key


1. Sort strings first, then compare

In JavaScript, characters are encoded in Unicode, so you can first convert a string to an array of characters and then sort the array of characters. If the result of sorting is the same, the two strings are alphabetic allowords.

/ * * *@param {string} s
 * @param {string} t
 * @return {boolean}* /
var isAnagram = function(s, t) {

    if(s.length ! == t.length) {return false;
    }

    let sArr = s.split(' '),
        tArr = t.split(' ');

    / / sorting
    sArr.sort(function(a,b){
        return a.charCodeAt()-b.charCodeAt();
    });
    tArr.sort(function(a,b){
        return a.charCodeAt()-b.charCodeAt();

    });

    // compare the sorting results
    if(sArr.toString() === tArr.toString()) {
        return true;
    }else {
        return false; }};Copy the code


2. Record two strings using secondary group space

As soon as I see the question, I want to iterate over the two strings first, using a common object or Map to record the number of occurrences of each letter. If the common object or Map is the same, then the two strings are mutually allotopic words.

/ * * *@param {string} s
 * @param {string} t
 * @return {boolean}* /
var isAnagram = function(s, t) {

    if(s.length ! == t.length) {return false;
    }

    let sMap = new Map(),
        tMap = new Map(a);for(let item of s) {

        if(sMap.get(item) === undefined) {
            sMap.set(item,1);
        }else {
            sMap.set(item,sMap.get(item) + 1); }}for(let item of t) {

        if(tMap.get(item) === undefined) {
            tMap.set(item,1);
        }else {
            tMap.set(item,tMap.get(item) + 1); }}=== === === === === === === === =
    for(let [key,value] of sMap) {

            if( tMap.get(key) === undefined|| tMap.get(key) ! == value) {return false; }}return true;

};
Copy the code

To improve the

The above method uses two maps, the following one; One loop Map records the occurrence times of each character in the string, and the other loop traverses each character in the other string. If there are corresponding characters in the Map, the corresponding number is reduced by 1 until the number reaches 0, and the attribute is deleted from the Map. As follows:

/ * * *@param {string} s
 * @param {string} t
 * @return {boolean}* /
var isAnagram = function(s, t) {

    if(s.length ! == t.length) {return false;
    }

    let sMap = new Map(a);// Add attributes in the Map
    for(let item of s) {

        if(sMap.get(item) === undefined) {
            sMap.set(item,1);
        }else {
            sMap.set(item,sMap.get(item) + 1); }}// Delete attributes in the Map
    for(let item of t) {

        let value = sMap.get(item);
        if(value ! = =undefined) {

            sMap.set(item,value - 1)
            if(sMap.get(item) === 0) { sMap.delete(item); }}else {
            return false; }}if(sMap.size === 0) {
        return true;
        
    }else {
        return false; }};Copy the code


If you have a better way of thinking and reconciliation, welcome to discuss ah ~

This is a summary and implementation of each problem in LeetCode’s “Elementary Algorithms” using JavaScript. The summary is here:

Juejin. Cn/post / 700669…