Introduction

Hello, and welcome to our last lesson in this course. Today, we will be tackling a common problem in the field of string manipulations with Go. We will learn how to find all occurrences of a substring within a larger string. The techniques you will master today can be utilized in numerous situations, such as text processing and data analysis. Are you ready to get started? Let's dive in!

Task Statement and Description

Here is the task for today: we have two slices of strings, both of identical lengths — the first contains the "original" strings and the second contains the substrings. Our goal is to detect all occurrences of each substring within its corresponding original string and, finally, return a slice that contains the starting indices of these occurrences. Remember, the index counting should start from 0.

Example: let's consider the following slices:

  • Original Slice: {"HelloWorld", "LearningGo", "GoForBroke", "BackToBasics"}
  • Substring Slice: {"loW", "ear", "o", "Ba"}.

The following are the expected outputs:

  • In "HelloWorld", "loW" starts at index 3.
  • In "LearningGo", "ear" starts at index 1.
  • In "GoForBroke", "o" appears at indices 1, 3, and 7.
  • In "BackToBasics", "Ba" starts at indices 0 and 6.

Thus, when findSubString([]string{"HelloWorld", "LearningGo", "GoForBroke", "BackToBasics"}, []string{"loW", "ear", "o", "Ba"}) is invoked, the function should return:

Let's break it down step by step.

Step 1 - Creating the Output Slice

Initially, we need to create a space to store our results. For this task, a slice of strings would be ideal.

Step 2 - Pairing Strings and Locating First Occurrence

To pair original strings with their substrings, we use a simple for loop. We can rely on a single loop index, as both slices share the same length. To find the first occurrence of each substring in the corresponding original string, we utilize the strings.Index function:

The strings.Index function returns the index of the first occurrence of the substring or -1 if it is not present.

Step 3 - Locating Subsequent Occurrences

The next step is to find subsequent instances of the substring in the original string.

To do this, we will again use a for loop. The loop will continue until strings.Index returns -1, which signifies there are no more matches to be found. Each time we locate a match, we record its starting index in the matchIndices slice, adjust the startPos, and begin the search anew:

Step 4 - Formatting and Storing the Results

Finally, we use fmt.Sprintf to format the results for improved readability and add them to the result slice:

That's it! We have completed the design of our function.

Complete Solution

Here is the complete function, incorporating all the steps we have discussed so far:

Lesson Summary

Well done! You've mastered a central operation in string manipulations in Go — finding all occurrences of a substring in another string. Keep in mind that this algorithm has numerous applications in real-world scenarios. Now that we have intricately dissected the problem and provided a detailed solution, I encourage you to practice more. Future exercises will help you hone your skills further. Keep on coding and exploring!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal