Skip to content
Discussion options

You must be logged in to vote

We can follow these steps:

  1. Break the sentences into words: Split both s1 and s2 into individual words.
  2. Count the occurrences of each word: Use an associative array (hash table) to count how many times each word appears across both sentences.
  3. Filter uncommon words: Find words that appear exactly once in the combined set of words from both sentences and do not appear in both sentences.

Approach:

  • Use the explode function to split the sentences into arrays of words.
  • Use an associative array to count the frequency of each word.
  • Return the words that have a count of 1.

Let's implement this solution in PHP: 884. Uncommon Words from Two Sentences

<?php
/**
 * @param String $s1
 * @param Strin…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 17, 2024
Maintainer Author

@basharul-siddike
Comment options

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants