Problem · String

Find Sliding-Window Pattern Start Indices

Learn this problem
EasyVisa logoVisaNEW GRADOA

Problem statement

You are given a lowercase string sequence and a nonempty lowercase string pattern.

Return every zero-based index at which a contiguous window of sequence is exactly equal to pattern. Return the indices in ascending order. Overlapping matches are allowed.

Function

findPatternStartIndices(sequence: String, pattern: String) → int[]

Examples

Example 1

sequence = "abracadabra"pattern = "abra"return = [0,7]

The exact pattern appears at the beginning and again starting at index 7.

Example 2

sequence = "aaaa"pattern = "aa"return = [0,1,2]

All three length-two windows match, including overlapping windows.

Example 3

sequence = "abc"pattern = "abcd"return = []

No window is long enough to equal the pattern.

Constraints

  • 0 <= sequence.length <= 200000.
  • 1 <= pattern.length <= 200000.
  • Both strings contain only lowercase English letters.

More Visa problems

drafts saved locally
public int[] findPatternStartIndices(String sequence, String pattern) {
  // write your code here
}
sequence"abracadabra"
pattern"abra"
expected[0,7]
checking account