Javascript String is one String permutation of another String

Description

Javascript String is one String permutation of another String

string1 = 'llowye';
string2 = 'yellow';

function permutation(string1, string2) {
  var array = [];
  if(string1.length !== string2.length) {
    return false;
  }/*from ww w  . j  a va  2  s .c o  m*/
  for (var i = 0; i<string1.length; i++) {
    for (var j = 0;j<string2.length; j++) {
      if (string1[i] == string2[j]) {
        array.push(string1[i]);
        break;
      }
    }
  }
  if (string1.length == array.length) {
    return true;
  }
  return false;
}

console.log(permutation(string1, string2));
var string1 = "dog";
var string2 = "god";

var perm = function(string1, string2) {
  a = [];//from ww  w  . j a  v a  2  s  .  c  om
  b = [];
  for(var i=0; i<string1.length; i++) {
    for(var j=0; j<string2.length; j++) {
      if(string1[i] === string2[j]) {
        a.push(string1[i]);
        b.push(string2[j]);
        break;
      }
    }
  }
   if(string1.length === a.length && string2.length === b.length && a.length === b.length) {
      return true;
    } else {
      return false;
    }
};
console.log(perm(string1, string2));



PreviousNext

Related