Advertisement
Guest User

Untitled

a guest
Sep 27th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. const answer = (checkWords) => {
  2. let [firstWord, secondWord] = checkWords.split(' ');
  3. if (firstWord.length !== secondWord.length) {
  4. return 0;
  5. }
  6.  
  7. let replaceVocabulary = {};
  8.  
  9. const replaceWord = (letterNew, letterOld) => {
  10. //проверим, если буква для замены уже есть в строке, то заменим ее на другой символ, тупо берем символ, который находится на позиции +100 от текущего
  11. if (firstWord.includes(letterNew)) {
  12. const replaceExpression = new RegExp(letterNew, "g");
  13. const replaceSymbol = String.fromCharCode(letterNew.charCodeAt(0)+100)
  14. firstWord = firstWord.replace(replaceExpression, replaceSymbol);
  15. }
  16.  
  17. const replaceExpression = new RegExp(letterOld, "g");
  18. firstWord = firstWord.replace(replaceExpression, letterNew);
  19. }
  20.  
  21. for (let i = 1; i < secondWord.length+1; i++) {
  22. if (secondWord[i] !== firstWord[i] && !replaceVocabulary[secondWord[i]]) {
  23. replaceVocabulary[secondWord[i]] = true;
  24. replaceWord(secondWord[i], firstWord[i]);
  25. }
  26. }
  27.  
  28. return secondWord === firstWord ? 1 : 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement