Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task 2 — Multiple Substring Search

Objective

Create a function that searches one source string for multiple target strings.

For every target value, return all starting indexes where that value occurs.

The search algorithm must be implemented manually.

Input

The source string is:

s1 := "abc bca cba acb abc bca cba acb"

The first search value is:

s2 := "cba"

The second search value is:

s3 := "acb"

Function

Create a function named:

FindMatch(...)

Conceptually:

FindMatch(s1, s2, s3 string)

returns:

map[string][]int

Result Type

Each map key represents a searched string.

The value associated with that key contains all starting indexes where the string was found.

For example:

map[string][]int{
    "cba": {8, 24},
    "acb": {12, 28},
}

Search for cba

The value:

cba

occurs at:

8
24

Therefore:

"cba": {8, 24}

Search for acb

The value:

acb

occurs at:

12
28

Therefore:

"acb": {12, 28}

Expected Result

map[string][]int{
    "cba": {8, 24},
    "acb": {12, 28},
}

Requirements

For every search value:

  1. manually scan the source string
  2. locate every complete occurrence
  3. collect the starting indexes
  4. associate those indexes with the searched value

The indexes must use zero-based indexing.

Missing Values

If a search value does not occur in the source, preserve the searched value in the result with an empty index collection.

For example:

map[string][]int{
    "xyz": {},
}

This makes it possible to distinguish between:

a search that produced no matches

and:

a search that was never requested

Overlapping Matches

Overlapping matches should be detected.

For example:

source = "aaaa"
search = "aa"

should produce:

[]int{0, 1, 2}

Corrected Result Type

The original version of the task described the result as:

map[string]int

while simultaneously assigning multiple indexes to each key.

Since one searched value may occur multiple times, the corrected type is:

map[string][]int

This preserves the original task behavior while making the type consistent with its expected result.

Restricted Operations

Do not use helpers that directly perform substring searching, such as APIs equivalent to:

Index
Contains
Count
Find
regular expressions

The search logic must be implemented manually.

Implementation Notes

The search operation from Task 1 can be generalized and reused internally for each requested search value.

The result should not depend on map iteration order.

Scalionix Docs

Keyboard Shortcuts

Navigate the documentation without leaving the keyboard.
Navigation
Previous subject
←
Next subject
→
Previous subsection
Alt + ↑
Next subsection
Alt + ↓
Interface
Documentation Home
Ctrl + Enter
Search
Alt + Q
Open shortcuts
?
Close dialog
Esc
Scalionix Docs

Search Documentation