#C9671. Find the Shortest Distance Between Words
Find the Shortest Distance Between Words
Find the Shortest Distance Between Words
You are given a list of words and two target words word1 and word2. Your task is to find the shortest distance (in terms of list index difference) between these two words in the list.
Formally, if the indices of word1 and word2 in the list are i and j respectively, you need to minimize \(|i - j|\) over all possible occurrences of these words in the list.
It is guaranteed that both words exist in the list.
inputFormat
The input is read from standard input and has the following format:
- 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 the string word1.
- The fourth line contains the string word2.
outputFormat
Output a single integer representing the shortest distance between word1 and word2 as computed by the formula \(|i - j|\).
## sample5
practice makes perfect coding makes
coding
practice
3