- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Program to Find Longest Common Prefix Using Word by Word Matching
Prefixes are the substrings that start from the zeroth index of the given string and can be of any size from 1 to the complete length of the string. We are given a set of strings and we have to find the common prefix among all of them in the JavaScript programming language. We have to implement the word-by-word matching approach in which we will match the words instead of the complete strings.
Input
arr = ["zefkefgh", "zefkdefij", "zeffdzy", "zefkdabacd"];
Output
zef
Explanation
From all the given strings, we have the first three characters, the same and the remaining characters are not the same for all of them
Input
arr = ["zefkefgh", "bcdefij", "acdzy", "abzefkacd"]
Output
""
Explanation
In the given string, none of them shares the same prefix so we have returned the empty string.
Comparing All the Strings
Comparing all the strings means, we will take the first string as the initial string and then compare all the remaining strings with the current string and will get the smallest prefix as the answer.
Example
// creating a function to find the longest common prefix of two given strings function commonPre(str1, str2){ var ans = ""; var len1 = str1.length; // length of the string 1 var len2 = str2.length; // length of the string 2 // comparing both teh strings until they are same var i = 0; // pointer to traverse over both the string while(i < len1 && i < len2){ if(str1[i] != str2[i]){ // break the string if the current character is not same break; } else{ // add the current character to the answer string ans += str1[i]; } i++; } return ans; // return the answer } // helper function that will return the final answer function helper(arr, len){ var pre = arr[0]; // initializing the prefix variable // comparing the zeoth string with all the string for (var i = 1; i < len; i++){ pre = commonPre(pre, arr[i]); } return pre; // return the final answer } // given array var arr = ["zefkefgh", "zefkdefij", "zeffdzy", "zefkdabacd"]; var len = arr.length; // getting length of the given array var ans = helper(arr, len); // calling to the function if (ans.length > 0){ // answer string is not empty console.log("The longest common prefix among the given strings is: " + ans); } else{ // answer string is empty console.log("There is no common prefix among the given strings"); }
Output
The longest common prefix among the given strings is: zef
Time and Space Complexity
The time complexity of the above code is O(N*M), where N is the number of string and M is the length of the strings.
The space complexity of the above code is O(M), as we have used a string to store the common prefix.
Word By Word Matching
In this approach, we are going to traverse over the first string and will use the for loop to traverse over the array and check for the current character of all the strings if is the same as of the first string or not.
If the size of any string is equal to a current pointer or the value is not the same, then we will stop further searching otherwise we will add the current character to the answer and will move further.
In the end, we will return the string and will print the answer in the main function.
Example
// helper function that will return the final answer function helper(arr, len){ var ans = ""; // string to store the answer //traversging over the first string for (var i = 0; i < arr[0].length; i++) { var flag = true; // boolean variable to keep track // compare all the string with the first string current word for(var j = 1; j < len; j++){ if(arr[j].length == i){ // current string is smallest so, break the loop flag = false; break; } else if(arr[j][i] != arr[0][i]){ // current string's current character is not same flag = false; break; } else{ // the current character is same for both the strings continue; } } if (flag) { ans += arr[0][i]; } else { break; } } return ans; // returning the answer } // given array var arr = ["zefkefgh", "zefkdefij", "zeffdzy", "zefkdabacd"]; var len = arr.length; // getting length of the given array var ans = helper(arr, len); // calling to the function if (ans.length > 0){ // answer string is not empty console.log("The longest common prefix among the given strings is: " + ans); } else{ // answer string is empty console.log("There is no common prefix among the given strings"); }
Output
The longest common prefix among the given strings is: zef
Time and Space Complexity
The time complexity of the above code is O(N*M), where N is the number of strings and M is the length of the strings.
The space complexity of the above code is O(M), as we have used a string to store the common prefix.
Conclusion
In the above code, we have implemented a JavaScript program to find the common prefix of the given string. We have implemented two approaches one is the direct traversal approach by comparing all the strings and another is the word-by-word match approach. The time complexity of both strings is O(N*M).