当前位置:网站首页>Leetcode daily question - 28 Implement strstr() (simple)
Leetcode daily question - 28 Implement strstr() (simple)
2022-06-25 20:13:00 【Cheng Xiaoqi】
String matching problem : There are generally two methods : Violence matching method and kmp Algorithm
Method 1 : Violence solution
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
haystack_length = len(haystack)
needle_length = len(needle)
i = 0
while i+needle_length <= haystack_length:
flag = True
for j in range(0,needle_length):
if haystack[i+j] != needle[j]:
flag = False
break
if flag:
return i
i += 1
return -1
# Adapted from the official java Code

Time limit exceeded , Maybe the last instance is too long .
Method 2 :kmp Algorithm
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
a=len(needle)
b=len(haystack)
if a==0:
return 0
next=self.getnext(a,needle)
p=-1
for j in range(b):
while p>=0 and needle[p+1]!=haystack[j]:
p=next[p]
if needle[p+1]==haystack[j]:
p+=1
if p==a-1:
return j-a+1
return -1
def getnext(self,a,needle):
next=['' for i in range(a)]
k=-1
next[0]=k
for i in range(1,len(needle)):
while (k>-1 and needle[k+1]!=needle[i]):
k=next[k]
if needle[k+1]==needle[i]:
k+=1
next[i]=k
return next
# This code refers to 「 Random code recording 」
Such a classic kmp The algorithm is really brain - burning , So I decided to write a separate article kmp Summary of the algorithm
边栏推荐
- How to understand var = a = b = C = 9? How to pre parse?
- Huawei fast application access advertising service development guide
- Life cycle function of composite API
- The error log of vscode connecting to the server shows the problem of "insufficient permission". Directly use root to connect
- Skills of CCF question 2
- Transunet reading notes
- Wechat applet cloud function does not have dependency option installed
- 200 OK (from memory cache) and 200 OK (from disk cache)
- [harmonyos] [arkui] how can Hongmeng ETS call pa
- String since I can perform performance tuning, I can call an expert directly
猜你喜欢

Wechat applet swiper simple local picture display appears large blank

Web components - Basics

Understanding C language structure pointer

Thymleaf template configuration analysis

Share a billing system (website) I have developed
![[harmonyos] [arkui] how can Hongmeng ETS call pa](/img/19/9d2c68be48417e0aaa0d27068a67ce.jpg)
[harmonyos] [arkui] how can Hongmeng ETS call pa

Profile path and name

Impact of Huawei application transfer and application claim on user identification

Yaml configuration

Print 1 cute every 100 milliseconds ~ with a running lantern effect
随机推荐
PHP FPM, workman, spoole, golang simple performance test
Read multiple associations from a field using delimiters in laravel
Suddenly found that the screen adjustment button can not be used and the brightness can not be adjusted
Curtain down and departure
Yaml configuration
Automatic fitting when the applet reaches the top
JS canvas drawing an arrow with two hearts
JS mobile phone and computer open different websites
206. reverse linked list (insert, iteration and recursion)
<C>. Calculation date to day conversion
VMware failed to prompt to lock this profile exclusively
8. iterators and generators
Native JS array some method de duplication
2.16([Usaco2005 Nov]Ant Counting)
<C>. function
Huawei fast application access advertising service development guide
PAT B1071
PAT B1081
Is it safe to open an online account for new bonds? What should be paid attention to
Can the stock account opened through qiniu school be used? Is the fund safe?