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!
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:
Go1{ 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 'LearningGo' 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}
Let's break it down step by step.
Initially, we need to create a space to store our results. For this task, a slice of strings would be ideal.
Go1func solution(origStrs, substrs []string) []string { 2 var result []string
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:
Go1 for i := range origStrs { 2 startPos := strings.Index(origStrs[i], substrs[i])
The strings.Index
function returns the index of the first occurrence of the substring or -1 if it is not present.
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:
Go1 var matchIndices []int 2 for startPos != -1 { 3 matchIndices = append(matchIndices, startPos) 4 startPos = strings.Index(origStrs[i][startPos+len(substrs[i]):], substrs[i]) 5 if startPos != -1 { 6 startPos += matchIndices[len(matchIndices)-1] + len(substrs[i]) 7 } 8 }
Finally, we use fmt.Sprintf
to format the results for improved readability and add them to the result
slice:
Go1 result = append(result, fmt.Sprintf("The substring '%s' was found in the original string '%s' at position(s) %v.", substrs[i], origStrs[i], matchIndices)) 2 } 3 return result 4}
That's it! We have completed the design of our function.
Here is the complete function, incorporating all the steps we have discussed so far:
Go1package main 2 3import ( 4 "fmt" 5 "strings" 6) 7 8func solution(origStrs, substrs []string) []string { 9 var result []string 10 11 for i := range origStrs { 12 startPos := strings.Index(origStrs[i], substrs[i]) 13 var matchIndices []int 14 15 for startPos != -1 { 16 matchIndices = append(matchIndices, startPos) 17 startPos = strings.Index(origStrs[i][startPos+len(substrs[i]):], substrs[i]) 18 if startPos != -1 { 19 startPos += matchIndices[len(matchIndices)-1] + len(substrs[i]) 20 } 21 } 22 23 result = append(result, fmt.Sprintf("The substring '%s' was found in the original string '%s' at position(s) %v.", substrs[i], origStrs[i], matchIndices)) 24 } 25 26 return result 27} 28 29func main() { 30 result := solution([]string{"HelloWorld", "LearningGo", "GoForBroke", "BackToBasics"}, []string{"loW", "ear", "o", "Ba"}) 31 for _, res := range result { 32 fmt.Println(res) 33 } 34}
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!