Leetcode Note: Go - Uncommon Words From Two Sentences

Uncommon Words from Two Sentences - LeetCode
https://leetcode.com/problems/uncommon-words-from-two-sentences/

  • Go 言語で取り組んだメモ

所感

  • string s1, s2 が渡されるので uncommon words を string 配列で return する

回答

Uncommon Words from Two Sentences - LeetCode
https://leetcode.com/problems/uncommon-words-from-two-sentences/solution/

Two hashmap solution - LeetCode Discuss
https://leetcode.com/problems/uncommon-words-from-two-sentences/discuss/2458258/Two-hashmap-solution

func uncommonFromSentences(s1 string, s2 string) []string {
    str1 := strings.Split(s1, " ")
    str2 := strings.Split(s2, " ")
    
    wordSet1, wordSet2 := make(map[string]int), make(map[string]int)
    
    for _, word := range str1 {
        wordSet1[word]++
    }
    
    for _, word := range str2 {
        wordSet2[word]++
    }
    
    var res []string
    
    for word, fre := range wordSet1 {
        if _, ok := wordSet2[word]; !ok && fre == 1 {
            res = append(res, word)
        }
    }
    
    for word, fre := range wordSet2 {
        if _, ok := wordSet1[word]; !ok && fre == 1 {
            res = append(res, word)
        }
    }
    
    return res
}