Skip to content

Instantly share code, notes, and snippets.

@mrmlnc
Last active March 29, 2017 20:06
Show Gist options
  • Save mrmlnc/110f3eafd489fd00bd7f2ee96d369936 to your computer and use it in GitHub Desktop.
Save mrmlnc/110f3eafd489fd00bd7f2ee96d369936 to your computer and use it in GitHub Desktop.
function hasLetter(dict: object, letter: char): boolean {
return dict.hasOwnProperty(letter);
}
function updateLetterStat(dict: object, letter: char): void {
if (!hasLetter(dict, letter)) {
dict[letter] = 0;
}
dict[letter] += 1;
}
function isAnagram(a: string, b: string): void {
const dictionary = {};
if (a.length !== b.length) {
console.log(false);
return;
}
if (a === b) {
console.log(true);
return;
}
for (let i = 0; i < a.length; i++) {
const aChar = a[i];
const bChar = b[i];
updateLetterStat(dictionary, aChar);
updateLetterStat(dictionary, bChar);
}
const keys = Object.keys(dictionary);
for (let i = 0; i < keys.length; i++) {
const char = keys[i];
const value = dictionary[char];
if (value % 2 !== 0) {
console.log(false);
return;
}
}
console.log(true);
}
isAnagram('стационар', 'соратница');
isAnagram('покраснение', 'пенсионерка');
isAnagram('покраснение', 'пенсионеркq');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment