Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const answer = (checkWords) => {
- let [firstWord, secondWord] = checkWords.split(' ');
- if (firstWord.length !== secondWord.length) {
- return 0;
- }
- let replaceVocabulary = {};
- const replaceWord = (letterNew, letterOld) => {
- //проверим, если буква для замены уже есть в строке, то заменим ее на другой символ, тупо берем символ, который находится на позиции +100 от текущего
- if (firstWord.includes(letterNew)) {
- const replaceExpression = new RegExp(letterNew, "g");
- const replaceSymbol = String.fromCharCode(letterNew.charCodeAt(0)+100)
- firstWord = firstWord.replace(replaceExpression, replaceSymbol);
- }
- const replaceExpression = new RegExp(letterOld, "g");
- firstWord = firstWord.replace(replaceExpression, letterNew);
- }
- for (let i = 1; i < secondWord.length+1; i++) {
- if (secondWord[i] !== firstWord[i] && !replaceVocabulary[secondWord[i]]) {
- replaceVocabulary[secondWord[i]] = true;
- replaceWord(secondWord[i], firstWord[i]);
- }
- }
- return secondWord === firstWord ? 1 : 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement