-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrString.py
107 lines (88 loc) · 2.66 KB
/
strString.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
Input:
"a"
""
Output:
-1
Expected:
0
"""
class Solution:
def strStr(self, haystack, needle):
"""
:type haystack: str
:type needle: str
:rtype: int
"""
needleLocation = -1
haystackLength = len(haystack)
needleLength = len(needle)
if (haystack == needle) or (needle == ""):
needleLocation = 0
return needleLocation
for x in range(haystackLength - needleLength + 1):
sampleHaystack = ""
print("x = ", x)
for y in range(needleLength):
sampleHaystack += haystack[x+y]
print("y = ", y , "sampleHaystack =", sampleHaystack)
if (sampleHaystack == needle) and (needleLength != 0):
needleLocation = x
return needleLocation
if haystack == needle:
needleLocation = x
return needleLocation
Answer = Solution()
testHaystack = "hello"
testNeedle = "ll"
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = "hello"
testNeedle = "el"
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = "hello"
testNeedle = "he"
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = "hello"
testNeedle = "lo"
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = "hello"
testNeedle = "l7"
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = "hello"
testNeedle = "l"
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = ""
testNeedle = "l"
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = "hello"
testNeedle = ""
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))
testHaystack = ""
testNeedle = ""
print("haystack =", testHaystack)
print("needle = ", testNeedle)
print("Solution = ", Answer.strStr(testHaystack,testNeedle))