Uncommon Words from Two Sentences
给两个序列, 用空格隔开. 找两个序列中只出现一次的word. 先连起来, 然后再做.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Solution { public String[] uncommonFromSentences(String A, String B) { Map<String, Integer> map = new HashMap<>(); String C = A + " "+ B; List<String> list = new ArrayList<>(); for (String w: C.split(" ")) map.put(w, map.getOrDefault(w, 0) + 1); for (String s: C.split(" ")){ if(map.get(s) == 1) list.add(s); } String[] res = new String[list.size()]; for(int i = 0 ; i < res.length; i++) { res[i] = list.get(i); } return res; } } |