zoukankan      html  css  js  c++  java
  • JavaScript将数组包含某字符串内容的项组成新数组

    What is an anagram? Well, two words are anagrams of each other if they both contain the same letters. For example:

    'abba' & 'baab' == true
    'abba' & 'bbaa' == true
    'abba' & 'abbba' == false
    

    Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none.

    For example:
    console.log(anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada'])); // ['aabb', 'bbaa']
    console.log(anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer'])); // ['carer', 'racer']
    

    my answer:

    function anagrams(word, words) {
    var arrNew = [];
    for(var i = 0; i<words.length; i++){
    	if(word.split('').sort().join('')==words[i].split('').sort().join('')){
    		arrNew.push(words[i]);
    	}    
    }
    return arrNew;
    }
    
  • 相关阅读:
    van Emda Boas
    斐波那契堆
    NTT
    FFT
    KDTree
    扩展kmp
    kmp
    Dancing Links
    树的prufer编码
    有向图最小路径覆盖
  • 原文地址:https://www.cnblogs.com/kid2333/p/7455199.html
Copyright © 2011-2022 走看看