Ransom Note

    xiaoxiao2021-04-12  35

    Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.

    Each letter in the magazine string can only be used once in your ransom note.

    var canConstruct = function(r, m) {     if(r.length > m.length) {         return false     }     if(r.length == m.length && r != m){         return false     }     var obj = {};     for(var i = 0; i < m.length; i++) {         if(!obj[m[i]]){             obj[m[i]] = 1         }else{             obj[m[i]] ++;         }     }     for(var j = 0; j < r.length; j++) {         if(obj[r[j]] && obj[r[j]] >= 0) {             obj[r[j]] -- ;         }else{             return false         }     }     return true };

    转载请注明原文地址: https://ju.6miu.com/read-667135.html

    最新回复(0)