Find the Exact Individual Count of Array of String in Array of Sentences in JavaScript


In the given problem statement we have to find the exact individual count of the array of strings in the array of sentences with the help of Javascript functionalities. So we will solve using the for loops to get the required result.

Understanding the Problem

The problem at hand is to find the exact individual count for each string in an array of sentences with the help of Javascript. We will be given an array or string and an array of sentences. So we have to find out how many times every string is appearing in the given sentence. For example:

Suppose we an array of strings as -
Strings = [“car”, “cycle”, “bike”] and
Sentence = ['I have a car and a bike.''];

So if we compare and check for the strings in the sentence we will have 
{ car: 1, cycle: 0, bike: 1 }

In the above output example we can see there is 1 count for car, 0 count for cycle and 1 count for bike after checking in the given sentence.

Logic for the given Problem

To solve the given task we will employ a two step method. First we will initialize a count of zero for each string in the array of strings. After that we will iterate over every sentence in the array of sentences and count the occurrences of every string within the sentence. At the end we will have the individual count for every string.

For matching the strings in the sentence we will use regular expressions using word boundaries. This is the way we can count the string as a whole word and not as part of another word. Then we will use the match method to find all occurrences of the string in the sentence. Lastly we will have an object that contains the individual count for every string and the key will be the string itself and the value will be the count.

Algorithm

Step 1: As we have to find out the counts of the strings in the given sentence, we will start the program by defining the function and give it a name as countStrings. This function will have two parameters: first will be an array of strings as strings and second will be the array of sentences as sentences.

Step 2: After declaring the function for completing the given task next we will be defining the blank object and give it a name as counts. This object will store the counts for each string in the given sentence.

Step 3: Now we need to iterate the given strings array. So first we will set an initial count for each string to zero by using the counts variable.

Step 4: After iterating the strings now it's time to iterate the given sentence array. And for each sentence we will iterate over each string in the strings array. Inside this iteration, we will create a regular expression with the word boundaries around the string to match the exact word. And also we will use the match method to find all occurrences of the string in the given sentence.

Step 5: As we have declared a regular expression and match method, our next task is to verify the matches. If we have found a match then we will save the count in the count variable, else set the count value to 0.

Step 6: Following the above step we will increment the count for the current string in the counts object by adding count to the existing count and lastly we will return the counts object to show the number of counts for the given strings in the sentence.

Example

// Create a function to count the strings
function countStrings(strs, sentences) {
   const counts = {};

   for (let i = 0; i < strs.length; i++) {
      counts[strs[i]] = 0;
   }

   for (let i = 0; i < sentences.length; i++) {
      const sentence = sentences[i];
      for (let j = 0; j < strs.length; j++) {
         const string = strs[j];
         // Create a regular expression for word matching
         const regex = new RegExp('\b' + string + '\b', 'gi');
         const matches = sentence.match(regex);
         const count = matches ? matches.length : 0;
         counts[string] += count;
      }
   }

   return counts;
}

const strs = ['apple', 'banana', 'orange'];
const sentences = ['I have an apple and a banana.', 'She likes orange but i like banana.'];
const result = countStrings(strs, sentences);
console.log(result);

Output

{ apple: 0, banana: 0, orange: 0 }

Complexity

The time complexity for the code is O(k * n * p), in which k is the size of the sentence array, n is the size of the strings array, and p is the total number of characters in the sentences. The space complexity for the code is O(n), here n is the length of the strings array. So we can say that the complexity depends on the size of the sentences, strings array and also the length of individual strings and sentences.

Conclusion

As we have successfully implemented the code for finding the exact individual count of an array of string in an array of sentences. The code efficiently computed the counts as we have used regular expression and a match function to calculate the number of counts for each string. But for large inputs the time complexity can impact the performance.

Updated on: 14-Aug-2023

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements