@@ -75,6 +75,27 @@ substringSearch ("helloworld", "world");
7575substringSearch (" abcrxyzgf" , " xyz" );
7676```
7777
78+ ### Python Implementation
79+
80+ #### [ Solution] ( ./Python/bruteForce.py )
81+
82+ ``` py
83+ '''
84+ @author prateek3255
85+ @date 05/01/2018
86+ '''
87+
88+ def subStringSearch (string ,pattern ):
89+ for i in range (len (string)- len (pattern)):
90+ if string[i:i+ len (pattern)]== pattern:
91+ return i
92+ return - 1
93+
94+ print (subStringSearch(" helloworld" ," hello" ))
95+ print (subStringSearch(" helloworld" ," hop" ))
96+ print (subStringSearch(" abcrxyzgf" ," xyz" ))
97+ ```
98+
7899## B) Knuth-Morris-Pratt Algorithm
79100
80101### JavaScript Implementation
@@ -85,6 +106,53 @@ substringSearch ("abcrxyzgf", "xyz");
85106To Be Added
86107```
87108
109+ ### Python Implementation
110+
111+ #### [ Solution] ( ./Pyhton/KMP.py )
112+
113+ ``` py
114+ def kmp (string ,pattern ):
115+ n= len (string)
116+ m= len (pattern)
117+ lps= calculateLPS(pattern)
118+ i= 0
119+ j= 0
120+ while i< n:
121+ if pattern[j]== string[i]:
122+ i+= 1
123+ j+= 1
124+ if j== m:
125+ return i- j
126+ elif i< n and pattern[j]!= string[i]:
127+ if j!= 0 :
128+ j= lps[j- 1 ]
129+ else :
130+ i+= 1
131+ return - 1
132+
133+ def calculateLPS (pattern ):
134+ lps= [0 ]* len (pattern)
135+ i= 1
136+ lenPat= 0
137+ while i< len (pattern):
138+ if pattern[i]== pattern[lenPat]:
139+ lenPat+= 1
140+ lps[i]= lenPat
141+ i+= 1
142+ else :
143+ if lenPat!= 0 :
144+ lenPat= lps[lenPat- 1 ]
145+ else :
146+ lps[i]= 0
147+ i+= 1
148+ return lps
149+
150+ print (kmp(" helloworld" ," hello" ))
151+ print (kmp(" helloworld" ," hop" ))
152+ print (kmp(" ABABDABACDABABCABAB" ," ABABCABAB" ))
153+ ```
154+
155+
88156## C) Z Algorithm
89157
90158### JavaScript Implementation
0 commit comments