LeetCode.884- two sentences are not common words (Uncommon Words from Two Sentences)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/codeMas/article/details/90690705

This is the first book of pleasure and delight 338 update, the first 362 Pian original

01 questions and look ready

LeetCode algorithm introduced today is the first title in the Easy level 207 title (overall title number is 884). We are given two sentences A and B. (A sentence string of space-separated words. Each word only lowercase letters.) If a word in a sentence which is displayed only once, and does not appear in another sentence, the word is not common. Returns a list of all non-common words. You can return to the list in any order. E.g:

输入:A = “this apple is sweet”, B = “this apple is sour”
输出:[“sweet”,“sour”]

Input: A = "apple apple", B = "banana"
Output: [ "banana"]

Note :

  • 0 <= A.length <= 200

  • 0 <= B.length <= 200

  • A and B contains only spaces and lowercase letters.

The use of problem-solving tools that eclipse, jdk version used is 1.8, the environment is win7 64-bit systems, and test using the Java language.

02 The first solution

There are two required topics:

  • There were two or more words that do not, either in A or in B.

  • A word can not appear in the B, B is the word can not appear in A.

Another way is concerned, the final result of a long string array word appears. Therefore, we can use HashMap, as to each word key, its number of occurrences of value, given the string A, B on spaces split into an array of strings, wherein the traversing element, into a HashMap, then iterate through HashMap, the value added to the resulting array is the key element 1, and finally returns the result array.

public String[] uncommonFromSentences(String A, String B) {
    List<String> result = new ArrayList<String>();
    Map<String, Integer> map = new HashMap<String, Integer>();
    String[] arr = A.split(" ");
    for (String str : arr) {
        map.put(str, map.getOrDefault(str, 0)+1);
    }
    String[] arr2 = B.split(" ");
    for (String str : arr2) {
        map.put(str, map.getOrDefault(str, 0)+1);
    }
    for (String key : map.keySet()) {
        if (map.get(key) == 1) {
            result.add(key);
        }
    }
    return result.toArray(new String[result.size()]);
}

03 The second solution

We can also connect strings A and B together, using only a for loop to process words other thinking the same.

public String[] uncommonFromSentences2(String A, String B) {
    List<String> result = new ArrayList<String>();
    Map<String, Integer> map = new HashMap<String, Integer>();
    String[] arr = (A+" "+B).split(" ");
    for (String str : arr) {
        if (!str.isEmpty()) {
            map.put(str, map.getOrDefault(str, 0)+1);
        }
    }
    for (String key : map.keySet()) {
        if (map.get(key) == 1) {
            result.add(key);
        }
    }
    return result.toArray(new String[result.size()]);
}

04 Summary

Thematic algorithm has been continuous days more than six months , the algorithm of feature articles 207 + articles, public Number dialog box reply [ data structures and algorithms ], [ algorithm ], [ data structures ] either a keyword to obtain a series of articles Collection .

That's all, if you have any good solution ideas, suggestions or other issues, you can exchange comments below, thumbs up, message forwarding and support is the greatest reward for me!

Guess you like

Origin blog.csdn.net/codeMas/article/details/90690705