Introduction
Mutations is a coding challenge where we check if all the characters of the second string exist within the first string, ignoring case sensitivity. For example, in the array['hello', 'hey'], we need to verify if all letters fromhey are present inhello. This challenge often appears in interviews, assessing candidates' problem-solving abilities. In this article, we will explore this challenge in depth, not only solving it but also employing a breakdown approach for better comprehension.
As each coding challenge can be solved using any programming language, I'll be using JavaScript to tackle this one. While the code may vary somewhat or even significantly if you're attempting to solve it in another programming language, the core approach to solving the problem remains the same. If you encounter difficulties in solving this challenge using a different language, don't worry; you're in the right place. I'll provide you with ideas to make the challenge easier.
Remember, having prior knowledge of solving problems or challenges can be quite helpful and enables us to quickly tackle the problem. That's precisely what we'll do here: first, we'll learn the theoretical aspects, and then we'll apply it practically by writing code. Pay close attention to this, as it's a crucial part. Before going into my ideas about this challenge, let's examine a few examples
Examples
mutation(["hello", "hey"])
// output: false
mutation(["hello", "Hello"])
// output: true
mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])
// output: true
mutation(["Mary", "Army"])
// output: true
mutation(["Mary", "Aarmy"])
// output: true
Theoretical Solution
Step 1:Check whether all the characters of the second string are present in the first string. If they are, returntrue. See the example below.
(mutation(["hello", "Hello"]))
// output: true
Remember, we only need to verify if all characters of the second string are present in the first string. You can check this example:
mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])
// output: true
Step 2: Both strings are converted tolowercase to ensure a fair comparison, since JavaScript treats uppercase andlowercase characters as distinct.
Step 3: We returnfalse if the characters of the second string are not present in the first string.
Practical Solution
function mutation(arr) {
let str1 = arr[0].toLowerCase();
let str2 = arr[1].toLowerCase();
for (let i = 0; i < str2.length; i++) {
if (str1.indexOf(str2[i]) === -1) {
return false;
}
}
return true;
}
console.log(mutation(["hello", "Hey"]));
// output: false
Breaking Down the Solution
Above, we have a function namedmutation with one parameterarr (["hello", "hey"]), where our two strings will be stored. Later on, we will use this function to compare both strings for a solution. It is a fairly simple function, so it doesn't need to be simplified.
Now that we understand the function, it's time to begin the operation to solve it. As you know, we have an array with two strings, so we need to assign these two strings to two different variables. This will make our next step even easier. You can see the code below, where we accomplish this:
let str1 = arr[0].toLowerCase();
let str2 = arr[1].toLowerCase();
In JavaScript, we use indexes to access the elements of any array. That's why we used arr[0] forstr1 this allows us to retrieve the first element of the array as required. Similarly, we used arr[1] forstr2, enabling us to obtain the second element of the array.
As we know, both strings should be in the same case, eitherlowercase or uppercase, because lowercase and uppercase characters are considered different in JavaScript (for example, c andC are distinct; you can verify this using 'c' === 'C' which will return false). Therefore, it's important to convert both strings to the same case. To achieve this, we used the.toLowerCase() method to convert both str1 andstr2to lowercase.
After that, we need to start a loop to access characters ofstr2so that we can begin a condition. We used a for loop(i = 0means the loop starts from the first element ofstr2,i < str2.length means it will iterate until the end of the array, andi++ means it should increment by one step from the beginning to the end). Within the loop, we used an if/else condition to check whether all characters ofstr2 are present instr1. We achieved this by using the indexOf()method, which takes an argument for checking and returns an index value if the element exists in the string, and returns -1 if the values don't match.
Here, we usedif (str1.indexOf(str2[i]) === -1)condition. If the output from theindexOf method is-1, indicating that the element is not found in str1, then we return false.
for(let i = 0; i < str2.length; i++) {
if(str1.indexOf(str2[i]) === -1) {
return false;
}
}
Finally, we use return true if our indexOf has successfully obtained all the elements' indexes