Skip to content
Advertisement

node.js – Check if word has an added/changed letter to a previous word

I’m working on a command for my Discord bot that allows for a game to be played, in which the goal is to post words that either change one letter in the previous word, or add a letter. I’m using this function:

JavaScript

and it works fine if you change a letter, or add a letter to the end of the word (e.g. mat->math). But if you add a letter in the word (e.g. mat->malt), it says that the word doesn’t follow the rules, even though it does. How can I change the function so it also catches for added letters inside the words?

Advertisement

Answer

I think this is more easier to understand.

JavaScript

The first condition is true when add one character. The second conditions is true when there is only one different character.

  • Updated (I did misread your question)

To check change one letter, use longest common sequence(https://en.wikipedia.org/wiki/Longest_common_subsequence_problem). If two string has same length N and the length of longest common sequence is N-1, it means one letter is changed. If two string has length N and N+1, and the length of longest common sequesnce is N, it means one letter is added.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement