Code coverage report for lib/algorithms/1-strings/permutation.js

Statements: 81.25% (13 / 16)      Branches: 75% (9 / 12)      Functions: 100% (1 / 1)      Lines: 100% (12 / 12)      Ignored: none     

All files » lib/algorithms/1-strings/ » permutation.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  1 2 2 2   2   2 46     2 24 24     1     1
//1.3) Given two strings, is one a permutation of the other?
var isPermutation = function(one, two) {
	Iif (typeof one !== "string") throw "The given word is not a string";
	Iif (typeof two !== "string") throw "The given word is not a string";
	Iif (one.length != two.length) return false;
 
	var letters = [];
 
	for (var i = 0; i < one.length; i++) {
		letters[one[i].charCodeAt()] = letters[one[i].charCodeAt()] || 0 + 1;
	}
 
	for (var i = 0; i < two.length; i++) {
		letters[two[i].charCodeAt()] = letters[two[i].charCodeAt()] || 0 - 1;
		if(letters[two[i].charCodeAt()] < 0) return false;
	}
 
	return true;
};
 
module.exports = isPermutation;