Common Character Count in Strings in JavaScript


In the given problem statement we are required to find the common characters count for the given strings with the help of Javascript functionalities. So we will use basic Javascript to solve this problem.

Understanding the problem

The problem at hand is to find the common characters between the two given strings. So for solving this problem we will determine how many characters they have in common. For example suppose we have two strings like "abaac" and "baaaa" so in these two strings we can see there are 2 common characters which are 'a' and 'b'. So the resultant output will be 2.

Logic for the given problem

To solve the given problem we will create a function to do the given task. And inside this function we will employ a very simple method that iterates over each character in one string and checks if the character exists in the second string. And if a character is found then we will increase a count variable by 1 and remove the character from the second string. This step will ensure that we don't count the same character multiple times.

Algorithm

Step 1: As we know that we have to calculate the count of common letters from the given two strings. So for doing this task we will first create a function and give it a name as commonCharacterCount and in this function we will pass two parameters str1 and str2. So basically we will compare these two strings str1 and str2 and find the common letters.

Step 2: After defining the function, inside this function we will initialize a count variable and put its initial value as zero.

Step 3: And now we will iterate over every character of the first string. Check the condition that the character exists in the second string so remove that character from the second string and also increment the value of count variable by one.

Step 4: At the end, we will return the count variable which represents the number of common characters.

Example

//Function to get the common character counts
function commonCharCount(str1, str2) {
   let count = 0;
   const arr1 = str1.split('');
   const arr2 = str2.split('');

   for (let i = 0; i < arr1.length; i++) {
      const char = arr1[i];
      const index = arr2.indexOf(char);

      if (index !== -1) {
         arr2.splice(index, 1);
         count++;
      }
   }

   return count;
}

const str1 = "Natasha";
const str2 = "Nitisha";
console.log(commonCharCount(str1, str2));

Output

5

Complexity

The time complexity for counting the common characters between the given strings is O(n^2), in which n is the length of the given input strings. Because for every character in the first string we need to search over the entire second string. And the space complexity for the problem is O(n). Because we have converted the strings into arrays.

Conclusion

In this function we have achieved the common character count problem in Javascript. As we have traversed through each character in one string and checked its presence in the second string. So we were able to find the number of common characters efficiently.

Updated on: 14-Aug-2023

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements