Algo. Anagrams

Is 2 strings are anagrams ? Decision through hash. Complexity O(n).

const s = 'abc';
const s1 = 'cba';

const s2 = 'ac';
const s3 = 'bb';

const hash = (str) => {
let res = 0;  
for (i = 0; i < str.length; i++) {   
  numValue  = str.charCodeAt(i);    
  res = res + (numValue * numValue - numValue - 97) * 35; // to be continued...  
  }   
  return res;  
}
  

document.body.innerHTML = 
  `<div>
 
  <p>${s} = ${s1}   ${(hash(s) === hash(s1))}</p>
  <p>${s2} = ${s3}  ${(hash(s2) === hash(s3))}</p>
  
  </div> `
This entry was posted in Без рубрики. Bookmark the permalink.