-
[Codewars] Which are in?Algorithm 2021. 3. 15. 18:28
www.codewars.com/kata/550554fd08b86f84fe000a58/train/javascript
Codewars: Achieve mastery through challenge
Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.
www.codewars.com
더보기문제
a 배열에 있는 문자가 b 배열에 속해 있으면 그 문자들을 리턴(사전 순서로)
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 []
풀이
- for문을 a 배열 길이, b 배열 길이만큼 사용한다.
- a 문자가 b 문자에 속해 있으면 a문자를 result 배열에 넣는다.
- 반복문이 끝나면 result 배열에 중복을 제거하고 배열로 만들고 정렬하여 리턴
function inArray(array1,array2){ let sortA = array1.sort(); let sortB = array2.sort(); let result = []; for(let i = 0; i < array2.length; i++) { for(let j = 0; j < array1.length; j++) { if(array2[i].indexOf(array1[j]) !== -1) { result.push(array1[j]); } } } // 중복 제거 let filter = new Set(result); // 배열로 let arrFilter = [...filter] return arrFilter.sort(); }
'Algorithm' 카테고리의 다른 글
[프로그래머스] 소수 만들기 (0) 2021.03.18 [Codewars] Sum of Digits / Digital Root (0) 2021.03.17 [프로그래머스] 예산 (0) 2021.03.11 [프로그래머스] 최대공약수와 최소공배수 (0) 2021.03.10 [프로그래머스] 2016년 (0) 2021.03.10