# Python finds substrings in strings

Posted by CrusaderSean on Thu, 10 Feb 2022 12:03:57 +0100

This is a question that Xiaobai blogger encountered when brushing leetcode. This is the best question in the leetcode question bank recently, so I share this joy here.

I hope I can continue to make progress and persevere in the future. catalogue

Topic introduction

Problem solving ideas and codes

1. Call function - find/index

2. Violence matching - double pointer

3. Violence matching - single pointer

## Topic introduction

This question is a simple question (28) in the leetcode question bank. The description of the question is as follows:

Implement the strStr() function.

Here are two strings: haystack and need. Please find the first position of the need string in the haystack string (the subscript starts from 0). If it does not exist, - 1 is returned. Source: LeetCode

## Problem solving ideas and codes

Here are four solutions for readers' reference:

### 1. Call function - find/index

(1) find function:

```str.find(str, beg=0, end=len(string))
```
• str -- specifies the string to retrieve
• beg -- start indexing, the default is 0.
• End -- end the index. The default is the length of the string.
• If beg and end are omitted, the default value is taken.

Returns the index value if it contains a substring, otherwise - 1.

(2) index function:

`str.index(str, beg=0, end=len(string))`
• str -- specifies the string to retrieve
• beg -- start indexing, the default is 0.
• End -- end the index. The default is the length of the string.
• If beg and end are omitted, the default value is taken.

If it contains a substring, return the index value at the beginning. Otherwise, throw an exception, and return: ValueError: substring not found

Therefore, the code of this problem-solving idea is as follows:

```class Solution:
def strStr(self, haystack: str, needle: str) -> int:
S = haystack
return S.find(needle)```
```class Solution:
def strStr(self, haystack: str, needle: str) -> int:
S = haystack
return S.index(needle)
```

Note: in example 2, if the substring is not found, it should return - 1. The find function meets this requirement, but the index function does not. Therefore, it is obvious that the find function is more suitable for this topic. Here we will also briefly introduce the rfind and rindex functions, which are very similar to the find and index functions. The difference is that the rfind/rindex function returns the index position of the last occurrence of the string, while the find/index function returns the index position of the first occurrence of the string.

```class Solution:
def strStr(self, haystack: str, needle: str) -> int:
S = haystack
return S.index(needle)
solution = Solution()
print(solution.strStr("albjnbl", "l"))

>>>1```
```​
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
S = haystack
return S.rindex(needle)
solution = Solution()
print(solution.strStr("albjnbl", "l"))

>>>6
​```

### 2. Violence matching - double pointer As shown in the figure above, we can convert the parent string Middle and substring Substrings of equal length are compared with each bit of need one by one.

• Time complexity: , where Is a string The length of the, Is a string The length of the. In the worst case, we need to put the string And string All lengths of All substrings match once.
• Space complexity: . We only need to store several variables in the constant space.

### 3. Violence matching - single pointer

Python can directly compare whether two strings are the same, so we can directly compare whether two substrings are equal without comparing the elements in the string one by one.

```class Solution:
def strStr(self, haystack: str, needle: str) -> int:
len1 = len(haystack)
len2 = len(needle)
if len2 == 0:
return 0
if len2 > len1:
return -1
else:
for i in range(len1 - len2 + 1):
fal_needle = haystack[i:i+len2]
if fal_needle == needle:
return i
return -1``` ## 4.KMP algorithm

Update hhh after the blogger understands it~