AdjventJS 2023: Day 3 Challenge

AdjventJS 2023: Day 3 Challenge

Fixing the modified mischievous elf steps!

·

4 min read

The solution to Challenge #3 of AdventJS 2023

The solution to the previous challenge

The solution to the next challenge

Challenge Description

In Santa's workshop, a mischievous elf has been playing around with the gift production line, adding or removing an unplanned step.

You have the original sequence of original manufacturing steps and the modified modified sequence that may include an extra step or be missing a step.

Your task is to write a function that identifies and returns the first extra step that was added or removed in the manufacturing chain. If there is no difference between the sequences, return an empty string.

const original = 'abcd'
const modified = 'abcde'
findNaughtyStep(original, modified) // 'e'

const original = 'stepfor'
const modified = 'stepor'
findNaughtyStep(original, modified) // 'f'

const original = 'abcde'
const modified = 'abcde'
findNaughtyStep(original, modified) // ''

Please, keep in mind:

  • There will always be one different step or none.

  • The modification can occur anywhere in the string.

  • The original steps could be empty

Analysis

The goal is to write a function that can identify the first difference between two strings, either by an extra char in the string or a missing char.

💡
Char: In programming, the char data type is for characters. It refers to a symbol (letters, numbers, punctuation) like a, +, 6, !, S. In JavaScript, it is still a string, as there is no data type for char, but when we talk about it we refer to a single character. - MDN Glossary

Input:

  1. Original (original): The original string, from which we must start to find the differences. These are the original steps.

  2. Modified (modified): The modified string to which we must find the first extra char or the first missing char. These are the steps modified by the mischievous elf.

Output:

  • A string showing the extra or missing char, if not found returns an empty string ''

Considerations

  • The modified sequence must not only contain the chars from the original but also have them in the same order.

Solution

You can use a loop to compare char by char in the strings and, upon finding the difference, evaluate whether it is an extra element or a missing element and return it. If the conditions are not met, we return an empty string ''.

Code

/**
 * Find the first different char between two strings.
 *
 * @param {string} original - The original string.
 * @param {string} modified - The modified string.
 * @return {string} The first difference between the original and modified string.
 */
function findNaughtyStep(original, modified) {
  // We start by declaring the index we will be iterating
  let i = 0;

  // While i is less than the sizes of both strings
  // we go through each one to compare them
  while (i < original.length || i < modified.length) {
    // If the char from the original string is different from the char in the modified string,
    // If they are the same we continue iterating and adding 1 to the index

    if (original[i] !== modified[i]) {
      // If the original string has more characters than the modified string,
      // return the char from the original string.
      // otherwise, return the char from the modified string
      return original.length > modified.length ? original[i] : modified[i];
    }

    i += 1;
  }

  return '';
}

Community Solutions

Solution by Achalogy:

findNaughtyStep(original, modified) {

  const [lessWords, mostWords] =
    [original, modified].sort((a, b) => a.length - b.length)

  return [...mostWords].find((x, i) => lessWords[i] != x) ?? "";
}

Solution by iswilljr:

findNaughtyStep(original: string, modified: string) {
  const originalLen = original.length
  const modifiedLen = modified.length

  const sequences = {
    [originalLen]: [original, modified],
    [modifiedLen]: [modified, original],
  }

  const [steps, reference] = sequences[Math.max(originalLen, modifiedLen)]

  return [...steps].find((step, index) => step !== reference[index]) ?? ''
}

And that was the challenge for December 3rd and its solutions. Do you have another alternative solution? Leave it in the comments!