@@ -457,6 +457,43 @@ To Be Added
457457To Be Added
458458```
459459
460+ ### Python Implementation
461+
462+ #### [ Solution] ( ./Python/rabinKarp.py )
463+
464+ ``` py
465+ '''
466+ @author prateek3255
467+ @date 05/01/2018
468+ '''
469+
470+ def rabinKarp (string ,pattern ,q = 153 ):
471+ n= len (string)
472+ m= len (pattern)
473+ d= 256
474+ h= d** (m- 1 )% q
475+ p= 0
476+ t= 0
477+
478+ for i in range (0 ,m):
479+ p= (d* p+ ord (pattern[i]))% q
480+ t= (d* t+ ord (string[i]))% q
481+ for i in range (n- m+ 1 ):
482+ if p== t and string[i:i+ m]== pattern:
483+ return i
484+
485+ if i< (n- m):
486+ t= (d* (t- ord (string[i])* h)+ ord (string[i+ m]))% q
487+ if t< 0 :
488+ t+= q
489+ return - 1
490+
491+ print (rabinKarp(" helloworld" ," hello" ))
492+ print (rabinKarp(" helloworld" ," hop" ))
493+ print (rabinKarp(" ABABDABACDABABCABAB" ," ABABCABAB" ))
494+
495+ ```
496+
460497## C++ Implementation
461498
462499### [ Solution] ( ./C++/RabinKarp.cpp )
@@ -523,6 +560,39 @@ int main()
523560
524561```js
525562To Be Added
563+ ```
564+ ### Python Implementation
565+
566+ #### [ Solution] ( ./Python/boyerMoore.py )
567+
568+ ``` py
569+ '''
570+ @author prateek3255
571+ @date 05/01/2018
572+ '''
573+ def boyerMoore (string ,pattern ):
574+ n= len (string)
575+ m= len (pattern)
576+ i= 0
577+ while i<= n- m:
578+ k= m- 1
579+ j= m+ i- 1
580+ while string[j]== pattern[k]:
581+ k= k- 1
582+ j= j- 1
583+ if k== 0 :
584+ return i
585+ if pattern.rfind(string[j])== - 1 :
586+ i= j+ 1
587+ else :
588+ i= max (1 ,j- pattern.rfind(string[j]))
589+ return - 1
590+
591+ print (boyerMoore(" helloworld" ," hello" ))
592+ print (boyerMoore(" helloworld" ," hop" ))
593+ print (boyerMoore(" abcrxyzgf" ," xyz" ))
594+ print (boyerMoore(" ABABDABACDABABCABAB" ," ABABCABAB" ))
595+
526596```
527597
528598### Have Another solution?
0 commit comments