An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once - Wikipedia.
Programmatically, can you do this?
wordAnagrams("silent", "listen"); // true
wordAnagrams("samson", "ebere"); // "unequal word lengths"
wordAnagrams("njoku", "ebere"); // false
We want to explore three (3) ways we can achieve this. We will be focusing on word anagrams
only. In the future, we will look into sentence anagrams. The major objective is that both words should contain exactly the same amount of letters and these letters should re-occur the same amount of times in each word.
Prerequisite
To benefit from this article, you need to have basic understanding of javascript's string, object and array methods.
Let's do this using:
- sort(), length, toLowerCase(), if...statement, join()
function wordAnagrams(wordA, wordB) {
let newWordA = wordA.toLowerCase();
let newWordB = wordB.toLowerCase();
if (newWordA.length === newWordB.length) {
return (
[...newWordA].sort().join("") === [...newWordB].sort().join("")
);
}
return "unequal word lengths";
}
- sort(), length, hasOwnProperty(), JSON.stringify(), toLowerCase(), if...statement, for...of...loop
function wordAnagrams(wordA, wordB) {
let newWordA = wordA.toLowerCase();
let newWordB = wordB.toLowerCase();
function createWordObject(word) {
let wordObject = {};
for (char of [...word].sort()) {
if (wordObject.hasOwnProperty(char)) {
wordObject[char]++;
} else {
wordObject[char] = 1;
}
}
return wordObject;
}
if (newWordA.length === newWordB.length) {
wordAObject = createWordObject(newWordA);
wordBObject = createWordObject(newWordB);
return JSON.stringify(wordAObject) === JSON.stringify(wordBObject);
}
return "unequal word lengths";
}
- sort(), length, hasOwnProperty(), toLowerCase(), Object.keys(), Object.values(), push(), .every(), if...statement, for...loop, for...of...loop
function wordAnagrams(wordA, wordB) {
let newWordA = wordA.toLowerCase();
let newWordB = wordB.toLowerCase();
let result = [];
function createWordObject(word) {
let wordObject = {};
for (char of [...word].sort()) {
if (wordObject.hasOwnProperty(char)) {
wordObject[char]++;
} else {
wordObject[char] = 1;
}
}
return wordObject;
}
if (newWordA.length === newWordB.length) {
let wordAObject = createWordObject(newWordA);
let wordBObject = createWordObject(newWordB);
let wordAObjectKeys = Object.keys(wordAObject);
let wordBObjectKeys = Object.keys(wordBObject);
let wordAObjectValues = Object.values(wordAObject);
let wordBObjectValues = Object.values(wordBObject);
let wordObjectLength = wordAObjectKeys.length;
// check if wordA Object is equivalent to wordB Object
for (let i = 0; i <= wordObjectLength; i++) {
let check =
wordAObjectKeys[i] === wordBObjectKeys[i] &&
wordAObjectValues[i] === wordBObjectValues[i];
if (check) {
result.push(true);
} else {
result.push(false);
}
}
return result.every(currentValue => currentValue === true);
}
return "unequal word lengths";
}
Conclusion
There are many ways to solve problems programmatically. I will love to know other ways you solved yours in the comment section.
If you have questions, comments or suggestions, please drop them in the comment section.
You can also follow and message me on social media platforms.
Thank You For Your Time.
Top comments (0)