-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode_0013.py
82 lines (71 loc) · 2.71 KB
/
lintcode_0013.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 13 · Implement strStr() 字符串查找
# https://www.lintcode.com/problem/13/description
# For a given source string and a target string, you should output the first index(from 0) of target string in the source string.If the target does not exist in source, just return -1.
class Solution:
"""
@param source:
@param target:
@return: return the index
"""
def str_str1(self, source: str, target: str) -> int:
# Write your code here
#print('source:',source)
#print('target:',target)
sourceLen=len(source)
targetLen=len(target)
if targetLen==0:return 0
if targetLen>sourceLen:return -1
return source.index(target)
def str_str2(self, source, target):
sourceLen=len(source)
targetLen=len(target)
#print(sourceLen,targetLen)
if targetLen==0:return 0
if targetLen>sourceLen:return -1
for i in range(sourceLen - targetLen +1):
for j in range(targetLen):
if source[j+i]==target[j]:
if j==targetLen-1: return i
else: break
return -1
def str_str3(self, source, target):
# Write your code here
sLen=len(source)
tLen=len(target)
if sLen<tLen: return -1
if tLen==0: return 0
for s in range(sLen-tLen+1):
iMatch=True
for t in range(tLen):
if source[s+t]!=target[t]:
iMatch=False
break
if iMatch==True:
return s
return -1
def main():
S=Solution()
source = "abcdabcdefg"; target = "bcd"
print('------source={0:s},target={1:s}'.format(source,target))
print('str1:',S.str_str1(source,target))
print('str2:',S.str_str2(source,target))
print('str3:',S.str_str3(source,target))
source = "a"; target = "bcd"
print('------source={0:s},target={1:s}'.format(source,target))
print('str1:',S.str_str1(source,target))
print('str2:',S.str_str2(source,target))
print('str3:',S.str_str3(source,target))
source = "abc"; target = ""
print('------source={0:s},target={1:s}'.format(source,target))
print('str1:',S.str_str1(source,target))
print('str2:',S.str_str2(source,target))
print('str3:',S.str_str3(source,target))
source = "abc"; target = "edgef"
print('------source={0:s},target={1:s}'.format(source,target))
print('str1:',S.str_str1(source,target))
print('str2:',S.str_str2(source,target))
print('str3:',S.str_str3(source,target))
if __name__ == "__main__":
main()
#KMP
# https://blog.csdn.net/weixin_52622200/article/details/110563434