Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare two shuffled strings?

Tags:

javascript

I have the following two strings:

var str1 = "hello"; var str2 = "ehlol"; 

How can I check whether both strings contain the same characters?

like image 657
harry Avatar asked Feb 24 '16 05:02

harry


People also ask

Can I use == to compare two strings?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

What are the 3 ways to compare two string objects?

There are three ways to compare String in Java: By Using equals() Method. By Using == Operator. By compareTo() Method.

How do you check if a string is valid in shuffle of two strings?

Sort the string str and Compare str and str1. If str = str1, then string str1 is a shuffled substring of string str2. else repeat the above process till ith index of str2 such that (i +n – 1 > m)(as after this index the length of remaining string str2 will be less than str1.


2 Answers

May not be very optimal, but you can simply do

str1.split("").sort().join() == str2.split("").sort().join(); //outputs true 

Another suggested approach in one the comments (for optimization in case string length is quite big)

str1.length===str2.length && str1.split("").sort().join() == str2.split("").sort().join(); //first check the length to quickly rule out in case of obvious non-matches 
like image 105
gurvinder372 Avatar answered Sep 19 '22 06:09

gurvinder372


One of the recommended ways to do it is using a hash table: count how many times each character appears. Note that this works best if your characters are ASCII.

The complexity of this algorithm is O(M+N+sigma) where M, N are the lengths of the strings and sigma is the number of distinct letters. The complexity of the accepted solution is higher because of the sorting, which is usually done in O(N*logN), but still a good one if your strings are short. If your strings have hundreds of thousands of characters, then this is the way to go. The drawback of using hash tables is that the memory usage is higher than the solution that uses sorting.

function sameLetters(str1, str2){   var hash = {};    var len1 = str1.length;   var len2 = str2.length;    // Strings with different lengths can't contain the same letters   if(len1 !== len2) return false;    // Count how many times each character appears in str1   for(var i = 0; i < len1; ++i) {     var c =  str1[i];     if(typeof hash[c] !== 'undefined') hash[c]++;     else hash[c] = 1;   }    // Make sure each character appearing in str2 was found in str1   for(var i = 0; i < len2; ++i) {     var c =  str2[i];     if(typeof hash[c] === 'undefined') return false;     if(hash[c] === 0) return false;     hash[c]--;   }    // Make sure no letters are left   for(var c in hash) {     if(hash[c]) return false;   }        return true; } 

You can then call the function like this (play with it in the browser console):

sameLetters("hello", "ehlol"); // true sameLetters("hello", "ehllol"); // false 
like image 22
XCS Avatar answered Sep 20 '22 06:09

XCS