2021年4月9日星期五

IsAnagram, why - 97? - Javascript

I am trying to understand this isAnagram algorithm where two words need to have the same length and the same letters, Does anybody know why the -97 is required? Where does that number come from?

Why does it necessarily have to be the number 97?

var isAnagram = function(s, t) {      if (s.length !== t.length) return false;            let checkArray = new Array(26).fill(0);            for (let i = 0; i < s.length; i++) {          checkArray[s.charCodeAt(i) - 97]++;      }            for (let x = 0; x < t.length; x++) {          checkArray[t.charCodeAt(x) - 97]--;      }            for (let k = 0; k < 26; k++) {          if (checkArray[k] !== 0) return false;      }            return true;        };    console.log('Anagram: ', isAnagram("anagram","nagaram"))
https://stackoverflow.com/questions/67029910/isanagram-why-97-javascript April 10, 2021 at 09:05AM

没有评论:

发表评论