当前位置:网站首页>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
边栏推荐
- JS advanced
- PAT B1064
- Profile path and name
- Uniapp waterfall flow, applet waterfall flow, very simple, suitable for the whole platform
- 8. iterators and generators
- Measurement index SSMI
- H5 application conversion fast application
- Please do not call Page constructor in files
- Number of wechat applet custom input boxes
- Browser performance optimization (19)
猜你喜欢

JS canvas drawing an arrow with two hearts

Determine whether it is a web page opened on wechat

String since I can perform performance tuning, I can call an expert directly

Thymleaf template configuration analysis
![[harmonyos] [arkui] how can Hongmeng ETS call pa](/img/19/9d2c68be48417e0aaa0d27068a67ce.jpg)
[harmonyos] [arkui] how can Hongmeng ETS call pa

How to understand var = a = b = C = 9? How to pre parse?

Transunet reading notes

Clickhouse disables automatic clearing of tables / columns, that is, disables TTL

My official account writing experience sharing

Teach you how to add custom controls to a map
随机推荐
Pcl+vs2019+opencv environment configuration
Force wechat page font size to be 100%
The functions in the applet page are better than those in app JS first execution solution
Randomly generate 100 non repeating numbers between 1 and 150 and put them in the array
Modifying routes without refreshing the interface
Mqtt+ardunio+esp8266 development (excluding mqtt server deployment)
node. JS express connect mysql write webapi Foundation
2.1 write a program to calculate the sum and average of four integers.
PAT B1066
Wechat applet cloud function does not have dependency option installed
Dependency injection in PHP reflection implementation framework
Single chip microcomputer line selection method to store impression (address range) method + Example
PAT B1091
Life cycle function of composite API
206. reverse linked list (insert, iteration and recursion)
JS get the parameters in the URL link
The last core step of configuring theano GPU
Teach you how to add custom controls to a map
PAT B1057
Number of wechat applet custom input boxes