#C11800. Shortest Distance Between Two Words
Shortest Distance Between Two Words
Shortest Distance Between Two Words
Given a list of words and two specific target words, your task is to compute the shortest distance between these two words in the list. The distance is defined as the absolute difference between the indices of the words.
If the two target words are identical, then the distance is calculated between any two distinct occurrences of that word, and you should return the minimum distance found.
Note: The list of words can contain duplicates and the words may appear in any order.
The formula used when considering two indices i and j is: \( |i - j| \).
inputFormat
The input is read from stdin and is structured as follows:
- The first line contains an integer \( n \) representing the number of words in the list.
- The second line contains \( n \) space-separated words.
- The third line contains two space-separated strings:
word1
andword2
, the target words.
outputFormat
Output a single integer to stdout representing the shortest distance between the two target words.
## sample5
practice makes perfect coding makes
coding practice
3