Find all index pairs where two words appear within distance k in a text

Quick Overview

Given a text and two distinct words, list every pair of word positions where the first word and the second word are at most k words apart, sorted by position. The problem tests splitting text into words, exact matching, and producing all close pairs without comparing every combination of positions.

Find all index pairs where two words appear within distance k in a text

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `text`, two distinct words `word1` and `word2`, and an integer `k`, return all index pairs `[i, j]` where `word1` appears at word index `i` and `word2` appears at word index `j` in the text (when it is split by spaces), and the absolute distance $|i - j|$ is less than or equal to `k`. Indices count words, not characters, and are 0-based. Return the pairs sorted by the first index, then by the second index. ### Function Signature ```python def find_close_pairs(text: str, word1: str, word2: str, k: int) -> list[list[int]]: ... ``` ### Rules - The words of `text` are its maximal runs of letters, which is what Python's `text.split()` returns. Repeated, leading or trailing spaces never create empty words. - A word matches only when it equals `word1` or `word2` exactly, so `other` does not match `the`. - Each pair is `[index of word1, index of word2]`, even when the `word2` index is smaller. - If either word does not appear, or no pair is close enough, return an empty list. ### Constraints - `1 <= len(text) <= 10^4` - `1 <= len(word1), len(word2) <= 100` - `1 <= k <=` the number of words in `text` - `word1 != word2` - `text` consists of lowercase English letters and spaces. - `word1` and `word2` consist of lowercase English letters. ### Examples ```text Input: text = "the quick brown fox jumps over the lazy dog the fox", word1 = "the", word2 = "fox", k = 3 Output: [[0,3],[6,3],[9,10]] ``` `the` is at indices 0, 6 and 9, and `fox` is at indices 3 and 10. The pairs within distance 3 are (0, 3) and (6, 3), both at distance 3, and (9, 10) at distance 1. Every other combination is at least 4 apart. ```text Input: text = "a b a b a", word1 = "a", word2 = "b", k = 1 Output: [[0,1],[2,1],[2,3],[4,3]] ``` `a` is at indices 0, 2 and 4, and `b` is at indices 1 and 3. Each `a` is next to every `b` beside it, and the pairs (0, 3) and (4, 1) are 3 apart, so they are left out. ```text Input: text = " the other theory the", word1 = "the", word2 = "theory", k = 2 Output: [[0,2],[3,2]] ``` The extra spaces do not create words, so the words are `the`, `other`, `theory`, `the` at indices 0 to 3. `other` does not count as `the`, and both occurrences of `the` are within 2 of `theory` at index 2.

Overview: Given a text and two distinct words, list every pair of word positions where the first word and the second word are at most k words apart, sorted by position. The problem tests splitting text into words, exact matching, and producing all close pairs without comparing every combination of positions.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string text, two distinct words word1 and word2, and an integer k, return all index pairs [i, j] where word1 appears at word index i and word2 appears at word index j in the text (when it is split by spaces), and the absolute distance ∣i−j∣|i - j| is less than or equal to k.

Indices count words, not characters, and are 0-based. Return the pairs sorted by the first index, then by the second index.

Function Signature

def find_close_pairs(text: str, word1: str, word2: str, k: int) -> list[list[int]]:
    ...

Rules

  • The words of text are its maximal runs of letters, which is what Python's text.split() returns. Repeated, leading or trailing spaces never create empty words.
  • A word matches only when it equals word1 or word2 exactly, so other does not match the .
  • Each pair is [index of word1, index of word2] , even when the word2 index is smaller.
  • If either word does not appear, or no pair is close enough, return an empty list.

Constraints

  • 1 <= len(text) <= 10^4
  • 1 <= len(word1), len(word2) <= 100
  • 1 <= k <= the number of words in text
  • word1 != word2
  • text consists of lowercase English letters and spaces.
  • word1 and word2 consist of lowercase English letters.

Examples

Input: text = "the quick brown fox jumps over the lazy dog the fox", word1 = "the", word2 = "fox", k = 3
Output: [[0,3],[6,3],[9,10]]

the is at indices 0, 6 and 9, and fox is at indices 3 and 10. The pairs within distance 3 are (0, 3) and (6, 3), both at distance 3, and (9, 10) at distance 1. Every other combination is at least 4 apart.

Input: text = "a b a b a", word1 = "a", word2 = "b", k = 1
Output: [[0,1],[2,1],[2,3],[4,3]]

a is at indices 0, 2 and 4, and b is at indices 1 and 3. Each a is next to every b beside it, and the pairs (0, 3) and (4, 1) are 3 apart, so they are left out.

Input: text = " the  other theory  the", word1 = "the", word2 = "theory", k = 2
Output: [[0,2],[3,2]]

The extra spaces do not create words, so the words are the, other, theory, the at indices 0 to 3. other does not count as the, and both occurrences of the are within 2 of theory at index 2.