Permutation check - 2
Sort
by dpnminh
HTML
<div id="test">
</div>
JavaScript
function isPermutation(strA, strB){
var isValid = true;
//First validity check
if (!strA || !strB || strA.length !== strB.length){
return !isValid;
}
var sortedA = sort(strA), sortedB = sort(strB);
return sortedA === sortedB;
}
function sort(str){
return str.split("").sort().toString();;
}
function test(){
var tests = {
1: "DOOG vs DOGO - Result: " + isPermutation('DOOG', "DOGO"),
2: "DO G VS DOOG - Result: " + isPermutation('DO G', 'DOOG'),
3: "DOGY vs DOGE - Result: " + isPermutation('DOGY', 'DOGE'),
4: "gdoo and dGoo - Result: "+ isPermutation('gdoo', 'dGoo'),
5: "DOOG and GOOO - Result: "+ isPermutation('DOOG', 'GOOO'),
6: "DOOG and GOOD - Result: "+ isPermutation('DOOG', 'GOOD')
}
var strs = "";
for (var key in tests){
strs += "<div>" + tests[key] + "</div>";
}
document.getElementById('test').innerHTML = strs;
}
test();