Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1065: Index Pairs of a String

In this guide, we solve Leetcode #1065 Index Pairs of a String in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Given a string text and an array of strings words, return an array of all index pairs [i, j] so that the substring text[i...j] is in words. Return the pairs [i, j] in sorted order (i.e., sort them by their first coordinate, and in case of ties sort them by their second coordinate).

Quick Facts

  • Difficulty: Easy
  • Premium: Yes
  • Tags: Trie, Array, String, Sorting

Intuition

Prefix queries are most efficient with a trie.

Each character transitions to the next node in the tree.

Approach

Insert words into the trie and traverse by characters for queries.

Track terminal markers to distinguish full words from prefixes.

Steps:

  • Build the trie.
  • Traverse for each query.
  • Return matches or validations.

Example

Input: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"] Output: [[3,7],[9,13],[10,17]]

Python Solution

class Solution: def indexPairs(self, text: str, words: List[str]) -> List[List[int]]: words = set(words) n = len(text) return [ [i, j] for i in range(n) for j in range(i, n) if text[i : j + 1] in words ]

Complexity

The time complexity is O(total characters). The space complexity is O(total characters).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy