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.
#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 []
Notes:
Arrays are written in "general" notation. See "Your Test Cases" for examples in your language.
In Shell bash a1 and a2 are strings. The return is a string where words are separated by commas.
Beware: r must be without duplicates.
Don't mutate the inputs.
(요약) 첫 번째 배열 요소 중에서 두 번째 배열의 아무 문자열에 속해 있으면 그것들만 모아서 return
.
function inArray(array1,array2){ return array1.filter(str1 => { for(let i = 0; i < array2.length; i++) { if(array2[i].includes(str1)) { return true; } } }).sort(); }
array1
에서 요소를 뽑아야하니array1
을filter
로 걸러냄.
array2
를 돌면서 각 요소중array1
이 속한 문자열이 있으면 바로true
를return
해서 그 요소만 남김.마지막에
sort
로 정렬해서return
.