“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Today’s topic is simple. Before, even simple questions often forget to consider some factors and lead to multiple submissions. This time, it is too long to submit once.

A daily topic

Today’s daily topic is 884

  • A sentence is a string of words separated by Spaces. Each word consists only of lowercase letters.

  • A word is uncommon if it appears exactly once in one sentence but not in the other.

  • Given two sentences s1 and s2, return a list of all uncommon words. Returns a list of words that can be organized in any order.

Example 1:

Enter s1 ="this apple is sweet", s2 = "this apple is sour"Output:"sweet"."sour"]
Copy the code

Example 2:

Enter s1 ="apple apple", s2 = "banana"Output:"banana"]
Copy the code

Tip:

  • 1 <= s1.length, s2.length <= 200
  • S1 and S2 consist of lowercase letters and Spaces
  • Neither S1 nor S2 contain leading or trailing Spaces
  • All words in S1 and S2 are separated by a single space

Answer key

Today’s question is very similar to the 2,047 valid words in the sentence of the day before yesterday. Both of them need to traverse a sentence to get the word, but one is to judge whether the word is legal, and today is to judge whether the word appears many times.

Hash table

When it comes to determining whether something occurs more than once, the first thing that comes to mind is the map table. If the element that does not exist is assigned 1, then the value can be assigned 2 after the second occurrence.

And then we can define an array to hold the valid word, because after iterating through the first sentence, it’s a little bit more difficult to wait until the second sentence is iterated to see if it happens twice, and the point of the question is that regardless of the order of the words in the array, So we can first push the word into our array of answers when it appears once, and then remove it after it appears twice. Here we define an indexOf method to remove an array.

Array.prototype.indexOf = function(val) {
  for(var i = 0; i < this.length; i++) {
    if(this[i] == val)return i;
  }
  return -1;
};
Copy the code

If the number exists in the array, it returns its subscript; if it does not, -1 is returned. Therefore, we need to use it to determine whether the word exists in the array before removing it when traversing the sentence

if (ansArr.indexOf(word) + 1) {
  ansArr.remove(word);
}
Copy the code

Then there is our remove method remove:

Array.prototype.remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1); }};Copy the code

After that, we only need to iterate through two sentences, adding the first occurrence of the word to the answer array, and removing it from any sentence that occurs again:

/ * * *@param {string} s1
 * @param {string} s2
 * @return {string[]}* /
var uncommonFromSentences = function (s1, s2) {
  const s1Arr = s1.trim().split(/\s+/g);
  const s2Arr = s2.trim().split(/\s+/g);
  let map = new Map(a);let ansArr = [];
  for (const word of s1Arr) {
    if(! map.has(word)) { map.set(word,1);
      ansArr.push(word);
    } else {
      map.set(word, 2);
      if (ansArr.indexOf(word) + 1) { ansArr.remove(word); }}}for (const word of s2Arr) {
    if(! map.has(word)) { map.set(word,1);
      ansArr.push(word);
    } else {
      map.set(word, 2);
      if (ansArr.indexOf(word) + 1) { ansArr.remove(word); }}}return ansArr
};
Array.prototype.remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1); }};Copy the code