As reported: Goldman Sachs Analyst CoderPad round (GBM, Bengaluru, 2025): a long string
strand stringsstr1andstr2— find the minimum distance between an occurrence ofstr1and a later occurrence ofstr2. Matching is on whole tokens; there were many edge cases, debugged through the tests.
A text is split into words by whitespace. Given text, first and second, return the minimum value of j − i over all word positions i < j where word i is exactly first and word j is exactly second (whole words only: quickly does not match quick). Return -1 if second never appears after first.
Example 1
text = "the quick brown fox jumps over the lazy dog near the quick river"
first = "quick", second = "fox"
-> 2
Example 2
same text, first = "dog", second = "quick"
-> 3 dog is word 8, the second quick is word 11
Constraints: text has up to 10^5 words; words are non-empty; first and second are single words (they may be equal).
Code runner format: one JSON value per line: text, first, second. Implement wordDistance(text, first, second); the harness prints the integer.