Lesson 4
Mastering String Analysis with Scala: Finding Substring Occurrences
Introduction

Hello, and welcome to our analysis lesson. Today, we will be exploring 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 Scala. The techniques you will learn can be utilized 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 containing the "substrings." Our task is 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: List("HelloWorld", "LearningScala", "GoForBroke", "BackToBasics")
Substring List: List("loW", "ear", "o", "Ba").

This will produce the following outputs:
In "HelloWorld", "loW" starts at index 3.
In "LearningScala", "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 findSubStrings(List("HelloWorld", "LearningScala", "GoForBroke", "BackToBasics"), List("loW", "ear", "o", "Ba")) is called, the function should return

1List( 2 "The substring 'loW' was found in the original string 'HelloWorld' at position(s) 3.", 3 "The substring 'ear' was found in the original string 'LearningScala' at position(s) 1.", 4 "The substring 'o' was found in the original string 'GoForBroke' at position(s) 1, 3, 7.", 5 "The substring 'Ba' was found in the original string 'BackToBasics' at position(s) 0, 6." 6)
Step-by-Step Solution: Initializing the Output

First, we need to create a place to store the result of our findings. In Scala, we can use a ListBuffer to accumulate results, which can later be converted into an immutable List.

Scala
1import scala.collection.mutable.ListBuffer 2 3def solution(origStrs: List[String], substrs: List[String]): List[String] = { 4 val resultArr = ListBuffer[String]()
Pairing Strings and Finding First Occurrence

We use Scala's zip method to create pairs of original strings and substrings. We then use the indexOf method to find the first occurrence of each substring in its related original string. The indexOf method in Scala returns the index of the first occurrence of the substring if found, or -1 if not found.

Scala
1for ((original, substring) <- origStrs zip substrs) { 2 var startPos = original.indexOf(substring)
Finding Subsequent Occurrences

The next step is to locate the rest of the instances of the substring in the original. A while loop works well for this task. As long as startPos is not -1, there are more occurrences to find.

Scala
1val matchIndices = ListBuffer[String]() 2while (startPos != -1) { 3 matchIndices.append(startPos.toString) 4 startPos = original.indexOf(substring, startPos + 1) 5}
Formatting and Storing the Results

Finally, we format the result using string interpolation and append it to the resultArr:

Scala
1resultArr.append(s"The substring '$substring' was found in the original string '$original' at position(s) ${matchIndices.mkString(", ")}.")
The Final Solution

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

Scala
1import scala.collection.mutable.ListBuffer 2 3def solution(origStrs: List[String], substrs: List[String]): List[String] = { 4 val resultArr = ListBuffer[String]() 5 6 for ((original, substring) <- origStrs zip substrs) { 7 var startPos = original.indexOf(substring) 8 val matchIndices = ListBuffer[String]() 9 while (startPos != -1) { 10 matchIndices.append(startPos.toString) 11 startPos = original.indexOf(substring, startPos + 1) 12 } 13 resultArr.append(s"The substring '$substring' was found in the original string '$original' at position(s) ${matchIndices.mkString(", ")}.\n") 14 } 15 16 resultArr.toList 17}
Lesson Summary

Congratulations! You've mastered a vital operation in string manipulations — finding all occurrences of a substring in another string using Scala. 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 you with the opportunity to refine your skills. Keep on coding and learning!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.