title: Create new words by replacing all occurrences by letter pair
body: <p>Im trying to write a function that loops over a string and replaces all the occurrences of one letter with another and create new words of all possible combinations. The letters that should replace each other are a pair. Like s and z or v and f and are hard coded. The combinations should work both ways, e.i <code>sam -&gt; zam</code> and <code>zed -&gt; sed</code></p>
  <p>The order of the words in the set are not important.
  For the pairs <code>[s,z]</code> and <code>[v,f]</code> the word <code>stuffs</code> should generate
  <code>ztuffs, ztuvfs, ztuvfz, ztuffz, stuvvs, stuvvz, stuffz, stufvz</code>.
  (I think thats all possible combinations, but i may have missed some)</p>
  <p>I can't get it to work and it feels like I'm missing something, maybe the function should be recursive in some way? Or there is something i miss?</p>
  <p>First function I tried the more dynamic way by adding arrays of combinations and the latter it's just if statements doing the checks.</p>
  <p>This only replaces the first occurring letter in the set and not the other way around</p>
  <pre><code>console.log(replaceSimilarCharacters('stuffs')); // `[ 'stuffs', 'ztuffz', 'stuvvs' ]`
  
  const replaceSimilarCharacters = (word) =&gt; {
    let letterCombination = [
      ['s', 'z'],
      ['f', 'v']
  
    ];
    const set = new Set();
    set.add(word); // original word
    for (let i = 0; i &lt; word.length; i++) {
      letterCombination.forEach((letterCombo) =&gt; {
        letterCombo.forEach((letter) =&gt; {
          if (word[i] === letter) {
            const currentIndex = letterCombo.indexOf(letter);
            const nextIndex = (currentIndex + 1) % letterCombo.length;
  
            const nextLetter = letterCombo[nextIndex];
            const prevLetter = letterCombo[nextIndex - 1];
            const regex = new RegExp(letter, 'g');
  
            word = word.replace(regex, nextLetter);
            set.add(word);
            word = word.replace(regex, prevLetter);
            set.add(word);
          }
        });
      });
    }
  
    return Array.from(set);
  };
  </code></pre>
  <p>This works a bit better but still missing some combinations</p>
  <pre><code>console.log(replaceLettersInWord('stuffs')); // `[ 'stuffs', 'ztuffs', 'stuvfs', 'stufvs', 'stuffz' ]`
  
  const replaceLettersInWord = (word) =&gt; {
    const set = new Set();
    set.add(word);
  
    for (let i = 0; i &lt; word.length; i++) {
      const currentLetter = word[i];
  
      if (currentLetter === 'z') {
        set.add(setCharAt(word, i, 's'));
      }
      if (currentLetter === 's') {
        set.add(setCharAt(word, i, 'z'));
      }
      if (currentLetter === 'f') {
       set.add(setCharAt(word, i, 'v'));
      }
      if (currentLetter === 'v') {
       set.add(setCharAt(word, i, 'f'));
      }
    }
    return Array.from(set);
  };
  const setCharAt = (str, index, chr) =&gt; {
    if (index &gt; str.length - 1) return str;
    return str.substring(0, index) + chr + str.substring(index + 1);
  };
  </code></pre>