search

Home  >  Q&A  >  body text

javascript - A question from codewars, I can’t figure it out

Office:

Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.

Update

: Example 1:

a1 = ["arp", "live", "strong"]

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns["arp", "live", "strong"]

Example 2:

a1 = ["tarp", "mice", "bull"]

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
returns[]

Introduction

: Define function inArray(array1,array2){}

Description:

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]
a1 = ["xyz", "live", "strong"]
Test.assertSimilar(inArray(a1, a2), ["live", "strong"])
a1 = ["live", "strong", "arp"]
Test.assertSimilar(inArray(a1, a2), ["arp", "live", "strong"])
a1 = ["tarp", "mice", "bull"]
Test.assertSimilar(inArray(a1, a2), [])
世界只因有你世界只因有你2779 days ago916

reply all(3)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-19 10:33:57

    For reference:

    function inArray(a1, a2){
        return a1.filter(el_a1 => a2.find(el_a2 => el_a2.includes(el_a1))).sort();
    }

    reply
    0
  • 某草草

    某草草2017-05-19 10:33:57

    It should be a problem of finding the largest substring, please refer to the dynamic programming problem (2) - finding the longest common substring

    reply
    0
  • PHP中文网

    PHP中文网2017-05-19 10:33:57

    Simply put, return the a1 string that exists in a2, in the order of a1.

    For example, harp and sharp both have string arp. Both lively and alive have the string live, and amstrong has the string strong. Then the returned order corresponds to the order of a1, so return ["arp", "live", "strong"]

    reply
    0
  • Cancelreply