Which are in?
xGiven 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.
a1의 문자열이 포함된 단어가 a2에 있으면 해당되는 a1의 문자열들을 배열로 리턴한다.
x
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class WhichAreIn { public static String[] inArray(String[] array1, String[] array2) { String str = Arrays.toString(array2); List<String> ans = new ArrayList<String>(); for (int i = 0; i < array1.length; i++) { if (str.contains(array1[i])) ans.add(array1[i]); } String[] answer = ans.toArray(new String[ans.size()]); Arrays.sort(answer); return answer; }}
a2 전체를 String으로 만들어서 그 String 내에서 포함 여부를 확인했다.
'공부 > algorithm' 카테고리의 다른 글
| [LeetCode] #3 Longest Substring Without Repeating Characters (0) | 2019.12.05 |
|---|---|
| [LeetCode] #1 Two Sum (0) | 2019.12.05 |
| [codewars] Who likes it? (0) | 2019.03.24 |
| [codewars] number fun (0) | 2019.03.24 |
| [codewars] Dubstep (0) | 2019.03.23 |
댓글