Quick Overview

Given a DNA string and a list of short patterns, report every occurrence of every pattern, including overlapping matches, sorted by start position and then by pattern index. It tests multi-pattern string matching on large inputs, output-sensitive complexity, and careful handling of nested and repeated patterns.

Find every occurrence of multiple patterns in a DNA string

Company: 23andMe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given a DNA string `text` over the alphabet `{A, C, G, T}` and a list of query strings `patterns` over the same alphabet, find every occurrence of any pattern as a substring of `text`. Overlapping occurrences must all be reported. Each occurrence is a pair `[pattern_index, start_index]`, where `pattern_index` is the position of the matched pattern in `patterns` and `start_index` is the 0-indexed position in `text` where the match begins. Return all occurrences sorted by `start_index` ascending. When two occurrences share the same `start_index`, order them by `pattern_index` ascending. ### Function Signature ```python def find_occurrences(text: str, patterns: list[str]) -> list[list[int]]: ``` ### Rules - Each occurrence is a two-element list `[pattern_index, start_index]`. - Overlapping matches all count. A match of one pattern inside a match of another (such as `"A"` inside `"AA"`) is reported too. - `patterns` may contain the same string more than once. Each index is its own pattern and gets its own entries. - If nothing matches, return an empty list. ### Constraints - `1 <= len(text) <= 10^5` - `1 <= len(patterns) <= 10^4` - `1 <= len(patterns[i]) <= 100` - `text` and every `patterns[i]` contain only `'A'`, `'C'`, `'G'` and `'T'`. ### Examples ```text Input: text = "ACACAGT", patterns = ["ACA", "CAG", "GT"] Output: [[0, 0], [0, 2], [1, 3], [2, 5]] ``` "ACA" occurs at indices 0 and 2 (the two matches share the `A` at index 2), "CAG" occurs at index 3, and "GT" occurs at index 5. Sorting by start index gives the order shown. ```text Input: text = "AAAA", patterns = ["A", "AA"] Output: [[0, 0], [1, 0], [0, 1], [1, 1], [0, 2], [1, 2], [0, 3]] ``` "A" matches at 0, 1, 2 and 3, and "AA" matches at 0, 1 and 2. At each shared start index the pair for pattern 0 comes before the pair for pattern 1. ```text Input: text = "GATTACA", patterns = ["TA", "A", "TA"] Output: [[1, 1], [0, 3], [2, 3], [1, 4], [1, 6]] ``` "A" matches at 1, 4 and 6. "TA" matches once, at index 3, and since it appears twice in `patterns`, both index 0 and index 2 are reported at start 3.

Overview: Given a DNA string and a list of short patterns, report every occurrence of every pattern, including overlapping matches, sorted by start position and then by pattern index. It tests multi-pattern string matching on large inputs, output-sensitive complexity, and careful handling of nested and repeated patterns.

Given a DNA string `text` over the alphabet `{A, C, G, T}` and a list of query strings `patterns` over the same alphabet, find every occurrence of any pattern as a substring of `text`. Overlapping occurrences must all be reported. Each occurrence is a two-element list `[pattern_index, start_index]`, where `pattern_index` is the position of the matched pattern in `patterns` and `start_index` is the 0-indexed position in `text` where the match begins. Return all occurrences sorted by `start_index` ascending. When two occurrences share the same `start_index`, order them by `pattern_index` ascending. Implement `find_occurrences(text, patterns)`, which returns this list of occurrences. ### Rules - Each occurrence is a two-element list `[pattern_index, start_index]`. - Overlapping matches all count. A match of one pattern inside a match of another (such as `"A"` inside `"AA"`) is reported too. - `patterns` may contain the same string more than once. Each index is its own pattern and gets its own entries. - If nothing matches, return an empty list. ### Constraints - `1 <= len(text) <= 10^5` - `1 <= len(patterns) <= 10^4` - `1 <= len(patterns[i]) <= 100` - `text` and every `patterns[i]` contain only `'A'`, `'C'`, `'G'` and `'T'`. Every index in the output is below 10^5, so all returned values fit in a 32-bit signed integer. ### Example 1 ```text Input: text = "ACACAGT", patterns = ["ACA", "CAG", "GT"] Output: [[0, 0], [0, 2], [1, 3], [2, 5]] ``` "ACA" occurs at indices 0 and 2 (the two matches share the `A` at index 2), "CAG" occurs at index 3 and "GT" occurs at index 5. Sorting by start index gives the order shown. ### Example 2 ```text Input: text = "GATTACA", patterns = ["TA", "A", "TA"] Output: [[1, 1], [0, 3], [2, 3], [1, 4], [1, 6]] ``` "A" matches at 1, 4 and 6. "TA" matches once, at index 3, and since it appears twice in `patterns`, both index 0 and index 2 are reported at start 3, with pattern 0 first.

Constraints

  • 1 <= len(text) <= 10^5
  • 1 <= len(patterns) <= 10^4
  • 1 <= len(patterns[i]) <= 100
  • text and every patterns[i] contain only 'A', 'C', 'G' and 'T'.

Examples

Input: ('A', ['A'])

Expected Output: [[0, 0]]

Explanation: Minimum valid input: a one-character text equal to a one-character pattern gives one match at start 0.

Input: ('C', ['G'])

Expected Output: []

Explanation: Minimum valid input with no match returns an empty list.

Hints

  1. Any match that starts at a given index of `text` ends within the next 100 characters, because no pattern is longer than 100.
  2. Patterns that are the same string match at exactly the same start indices, so the matching work for a repeated string can be shared by all of its indices.
  3. At a single start index, patterns of different lengths can match at once, and their indices still have to come out in ascending pattern_index order.

Report Every Pattern Occurrence in a DNA String

hard23andMeSoftware Engineer

Given a DNA string text over the alphabet {A, C, G, T} and a list of query strings patterns over the same alphabet, find every occurrence of any pattern as a substring of text. Overlapping occurrences must all be reported.

Each occurrence is a two-element list [pattern_index, start_index], where pattern_index is the position of the matched pattern in patterns and start_index is the 0-indexed position in text where the match begins.

Return all occurrences sorted by start_index ascending. When two occurrences share the same start_index, order them by pattern_index ascending.

Implement find_occurrences(text, patterns), which returns this list of occurrences.

Rules

  • Each occurrence is a two-element list [pattern_index, start_index].
  • Overlapping matches all count. A match of one pattern inside a match of another (such as "A" inside "AA") is reported too.
  • patterns may contain the same string more than once. Each index is its own pattern and gets its own entries.
  • If nothing matches, return an empty list.

Constraints

  • 1 <= len(text) <= 10^5
  • 1 <= len(patterns) <= 10^4
  • 1 <= len(patterns[i]) <= 100
  • text and every patterns[i] contain only 'A', 'C', 'G' and 'T'.

Every index in the output is below 10^5, so all returned values fit in a 32-bit signed integer.

Example 1

Input: text = "ACACAGT", patterns = ["ACA", "CAG", "GT"]
Output: [[0, 0], [0, 2], [1, 3], [2, 5]]

"ACA" occurs at indices 0 and 2 (the two matches share the A at index 2), "CAG" occurs at index 3 and "GT" occurs at index 5. Sorting by start index gives the order shown.

Example 2

Input: text = "GATTACA", patterns = ["TA", "A", "TA"]
Output: [[1, 1], [0, 3], [2, 3], [1, 4], [1, 6]]

"A" matches at 1, 4 and 6. "TA" matches once, at index 3, and since it appears twice in patterns, both index 0 and index 2 are reported at start 3, with pattern 0 first.

Examples

Example 1
Input
('A', ['A'])
Output
[[0, 0]]
Notes
Minimum valid input: a one-character text equal to a one-character pattern gives one match at start 0.
Example 2
Input
('C', ['G'])
Output
[]
Notes
Minimum valid input with no match returns an empty list.

Constraints

  • •1 <= len(text) <= 10^5
  • •1 <= len(patterns) <= 10^4
  • •1 <= len(patterns[i]) <= 100
  • •text and every patterns[i] contain only 'A', 'C', 'G' and 'T'.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def find_occurrences(text: str, patterns: list[str]) -> list[list[int]]:
Sample call
find_occurrences('A', ['A'])
Language
Case 1
Input
('A', ['A'])
Expected
[[0, 0]]
Case 2
Input
('C', ['G'])
Expected
[]

Browse More Questions

Show the approach

Approach

Put every pattern into a trie over the four letters. Each trie node stands for one string, and a node where patterns end keeps the indices of every pattern equal to that string, so duplicate patterns share one node. Then take each start index of text in ascending order and walk down the trie one character at a time from that start. Whenever the walk reaches a node where patterns end, collect their indices. The walk stops when the next character has no child or the text runs out, so it never reads more than 100 characters, the length of the longest possible pattern.

Correctness: after reading text[start..pos], the walk sits on the node for exactly that substring, and such a node exists only when the substring is a prefix of some pattern. A pattern matches at start exactly when its string equals text[start..pos] for some pos, which is exactly when the walk passes the node where it ends. A pattern has one length, so it is collected at most once per start. Sorting the indices collected for one start gives pattern_index order inside that start, and the outer loop already visits starts in ascending order, so the whole result comes out in the required order without a global sort.

Edge cases: a pattern longer than the rest of the text is never reached, because the walk stops at the end of text. A pattern that is a prefix of a longer pattern is still reported, because indices are collected at every node on the path and not only at the deepest one. Identical patterns are all reported, because their shared end node keeps every index. When nothing matches, the result stays empty.

Time complexity:
O(P + n * L + K log m), where P is the total length of all patterns, n = len(text), L <= 100 is the longest pattern length, K is the number of reported occurrences and m = len(patterns)
Space complexity:
O(P + K)