From c70a6734a82044d3fe964ce547ece84ef14153e0 Mon Sep 17 00:00:00 2001 From: ductnn Date: Tue, 17 Sep 2024 22:57:49 +0700 Subject: [PATCH] add sol --- .../884.UncommonWordsfromTwoSentences/sol.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 leetcode/884.UncommonWordsfromTwoSentences/sol.go diff --git a/leetcode/884.UncommonWordsfromTwoSentences/sol.go b/leetcode/884.UncommonWordsfromTwoSentences/sol.go new file mode 100644 index 0000000..8a7aaf5 --- /dev/null +++ b/leetcode/884.UncommonWordsfromTwoSentences/sol.go @@ -0,0 +1,33 @@ +// https://leetcode.com/problems/uncommon-words-from-two-sentences/description/ + +package main + +import ( + "fmt" + "strings" +) + +func uncommonFromSentences(s1 string, s2 string) []string { + words := strings.Fields(s1 + " " + s2) + + wordCount := make(map[string]int) + for _, word := range words { + wordCount[word]++ + } + + var res []string + for word, count := range wordCount { + if count == 1 { + res = append(res, word) + } + } + + return res +} + +func main() { + s1 := "this apple is sweet" + s2 := "this apple is sour" + result := uncommonFromSentences(s1, s2) + fmt.Println(result) +}