Introduction

Hello, and welcome to our analysis lesson. Today, we will explore a classic problem in the realm of string manipulations. We'll learn how to locate all occurrences of a substring within a larger string using C#. The techniques you will learn can be used in scenarios such as text processing and data analysis. Are you ready to dive in? Let's get started!

Task Statement and Description

Here's our challenge: we have two lists of strings of the same length, one containing the "original" strings and the other, the "substrings." We're to identify all occurrences of each substring within its corresponding original string and return a list of the starting indices of these occurrences. Remember, index counting should start from 0.

Example

If we take the following lists:
Original List: new List<string> { "HelloWorld", "LearningCSharp", "GoForBroke", "BackToBasics" }
Substring List: new List<string> { "loW", "ear", "o", "Ba" }.

This will produce the following outputs:
In "HelloWorld", "loW" starts at index 3.
In "LearningCSharp", "ear" starts at index 1.
In "GoForBroke", "o" appears at indices 1, 3, and 7.
In "BackToBasics", "Ba" starts at indices 0 and 6.

So, if FindSubString(new List<string> { "HelloWorld", "LearningCSharp", "GoForBroke", "BackToBasics" }, new List<string> { "loW", "ear", "o", "Ba" }) is called, the function should return:

Although this task seems reasonably straightforward, it may still feel daunting. Fear not! We will dissect it piece by piece.

Step-by-Step Solution: Initializing the Output

First, we need to create a place to store the result of our findings. Can you think of a C# data type that we could use for that? That's correct — a List<string> would be perfect for this task!

Pairing Strings and Finding First Occurrence

We iterate over the pairs of original strings and substrings using a simple for loop. We then use the IndexOf() method to find the first occurrence of each substring in its related original string. C# string objects have a built-in method called str.IndexOf(substring, startingIndex) which returns the first index position of the substring starting from startingIndex, if found. Otherwise, it returns -1.

In original.IndexOf(substring), we pass the substring that we want to locate. The function begins the search from the beginning as we have not specified a starting position.

Finding Subsequent Occurrences

The following step is to locate the rest of the instances of the substring in the original.

We use a while loop to accomplish this. But, how will we know when to stop seeking more occurrences? Good question! The moment our .IndexOf() function returns -1, it signifies that there are no more matches to be found.

Each time we find a match, we append its starting index to the matchIndices list and adjust the startPos to search after the end of this match:

Formatting and Storing the Results

Finally, we format the result for readability and append it to the resultArr:

With this, we've come to the end of our method design.

The Final Solution

Here is the comprehensive solution, which incorporates all the steps above:

Lesson Summary

Congratulations! You've mastered a vital operation in string manipulations — finding all occurrences of a substring in another string using C#. Remember, this algorithm has numerous applications in real-world scenarios. Now that we have thoroughly dissected and examined a systematic way to handle it, I encourage you to practice further. Upcoming exercises will provide an opportunity for you to refine your skills better. Keep on coding and learning!

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