Mark As Completed Discussion

Good afternoon! Here's our prompt for today.

You're playing a game with a friend called Target and Vicinity. Target and Vicinity consists of one person thinking up a number in their head, and the other guessing that number.

Sounds simple enough-- the caveat is that we also want to credit close guesses. To estimate the proximity of the guess to the actual number, we use the concept of "targets" and "vicinities" in comparing accuracy.

Description

It works like this: targets are digits in the guessed number that have the same value of the digit in actual at the same position. Here's an example of two targets:

SNIPPET
1Actual: "34"
2Guess:  "34"
3"2T0V"

Notice that both the actual number and the guess have the same values, in the same position, with regards to 3 and 4.

Now onto "vicinities". Vicinities are digits in guess that have the same value as some digit in actual, but don't share the same position (hence the nomenclature).

Person 1 has to tell person 2 how many targets and vicinities there are by providing a string in this format:

JAVASCRIPT
1`${number of targets}T${number of vicinities}V"

Here are some examples of inputs and ouputs:

JAVASCRIPT
1const actual = "345"
2const guess  = "135"
3// "1T1V"
4// Because `5` is a T, and `3` is a V
5
6const actual = "45624"
7const guess  = "24325"
8// "1T2V"
9// Because `2` is a T, and `4` and `5` are Vs

You're given two strings, an actual and a guess, as parameters. Write a function to return the number of targets and vicinities in the above string format. You may assume the strings are of the same length.

Constraints

  • The actual and guess strings will be of length <= 100000
  • The strings will only consist of numeral digits 0 - 9
  • Expected time complexity : O(n)
  • Expected space complexity : O(n)

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

We'll now take you through what you need to know.

How do I use this guide?

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.