Go•uncommon-words-from-two-sentences/solution.go
package main
import "strings"
func uncommonFromSentences(s1 string, s2 string) []string {
counts := map[string]int{}
for _, word := range strings.Fields(s1 + " " + s2) {
counts[word]++
}
result := []string{}
for word, count := range counts {
if count == 1 {
result = append(result, word)
}
}
return result
}
Python•uncommon-words-from-two-sentences/solution.py
from collections import Counter
class Solution:
def uncommonFromSentences(self, s1: str, s2: str) -> list[str]:
counts = Counter((s1 + class="syntax-string">" " + s2).split())
return [word for word, count in counts.items() if count == class="syntax-number">1]
TypeScript•uncommon-words-from-two-sentences/solution.ts
function uncommonFromSentences(s1: string, s2: string): string[] {
const counts = new Map<string, number>();
for (const word of (s1 + class="syntax-string">" " + s2).split(class="syntax-string">" ")) counts.set(word, (counts.get(word) ?? class="syntax-number">0) + class="syntax-number">1);
return [...counts].filter(([, count]) => count === class="syntax-number">1).map(([word]) => word);
}