Last active
March 29, 2017 20:06
-
-
Save mrmlnc/110f3eafd489fd00bd7f2ee96d369936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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