package leetcode;

/**
 * https://leetcode.com/problems/uncommon-words-from-two-sentences/
 */
public class Problem888 {
    public String[] uncommonFromSentences(String A, String B) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem888 prob = new Problem888();
        System.out.println(prob.uncommonFromSentences("this apple is sweet", "this apple is sour")); // ["sweet","sour"]
        System.out.println(prob.uncommonFromSentences("apple apple", "banana")); // ["banana"]
    }
}

package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/uncommon-words-from-two-sentences/
 */
public class Problem888 {
    public String[] uncommonFromSentences(String A, String B) {
        Map<String, Integer> map = new HashMap<>();
        for (String str : A.split(" ")) {
            if (!map.containsKey(str)) {
                map.put(str, 1);
            } else {
                map.put(str, map.get(str) + 1);
            }
        }
        for (String str : B.split(" ")) {
            if (!map.containsKey(str)) {
                map.put(str, 1);
            } else {
                map.put(str, map.get(str) + 1);
            }
        }
        List<String> result = new ArrayList<>();
        for (Map.Entry<String, Integer> e : map.entrySet()) {
            if (e.getValue() == 1) {
                result.add(e.getKey());
            }
        }
        return result.toArray(new String[0]);
    }
}

Solve problem 888