09 Sept 2026Go / Python / TypeScriptEasy

Uncommon Words from Two Sentences

Return words that occur exactly once across two sentences combined.

Count all whitespace-separated words from both sentences in one map and keep entries with frequency one.

complexity

O(n + m) time and O(n + m) space.

solution files

  • Go uncommon-words-from-two-sentences/solution.go
  • Python uncommon-words-from-two-sentences/solution.py
  • TypeScript uncommon-words-from-two-sentences/solution.ts

Solution files

Gouncommon-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
}
Pythonuncommon-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]
TypeScriptuncommon-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);
}