We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
o is the minimum number of operations. But to deal with the one exception in this algorithm, we set it to the total length of both strings first. If the number of allowed operations is bigger than the sum of both strings, the answer will always be "Yes". So we check for that before moving along!
Now, we compare the letters in both strings and subtract each iteration from the minimum number of operations. Remember that each common letter means 2 less operations (remove a letter + add a letter). We break out of the loop once the letters aren't equal anymore.
Now, if the minimum number of operations is more than the allowed number, we return "No". Otherwise, as long as the remainder of the difference between minimum and allowed operations is even, we return "Yes".
Append and Delete
You are viewing a single comment's thread. Return to all comments →
Here's a short and sweet JavaScript solution if anybody wants it:
Explanation:
o is the minimum number of operations. But to deal with the one exception in this algorithm, we set it to the total length of both strings first. If the number of allowed operations is bigger than the sum of both strings, the answer will always be "Yes". So we check for that before moving along!
Now, we compare the letters in both strings and subtract each iteration from the minimum number of operations. Remember that each common letter means 2 less operations (remove a letter + add a letter). We break out of the loop once the letters aren't equal anymore.
Now, if the minimum number of operations is more than the allowed number, we return "No". Otherwise, as long as the remainder of the difference between minimum and allowed operations is even, we return "Yes".
Questions? =)