imashutosh51

KMP substring mathcing with string

Jul 21st, 2022 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package com.interview.string;
  2.  
  3.  
  4. // * Runtime complexity - O(m + n) where m is length of text and n is length of pattern
  5. // * Space complexity - O(n)
  6.  
  7. public class SubstringSearch {
  8.      //* Slow method of pattern matching Time complexity O(m*n)
  9.     public boolean hasSubstring(char[] text, char[] pattern){
  10.         int i=0;
  11.         int j=0;
  12.         int k = 0;
  13.         while(i < text.length && j < pattern.length){
  14.             if(text[i] == pattern[j]){
  15.                 i++;
  16.                 j++;
  17.             }else{
  18.                 j=0;
  19.                 k++;
  20.                 i = k;
  21.             }
  22.         }
  23.         if(j == pattern.length){
  24.             return true;
  25.         }
  26.         return false;
  27.     }
  28.    
  29.     /**           012345678910 11           01234567
  30.      * eg. string abcdabcdab c  y substring:abcdabcy
  31.      * start comparing both string from 0index,at index 7,string doesn't matches so comparing again from index 1 of string
  32.        and index 0 of string,is normal substring matching algo but we will search of prefix and suffix till index 6 in
  33.        substring and there exist a prefix and suffix in substring till index6,ie."abc" so we will not got back to 0th
  34.        position in substring,we will go to index4 in because before index4 element,position 3 to 6 will already have the
  35.        substring because, "abc" is suffix also.
  36.        
  37.        *for storing the prefix-suffix match,we will make an temporary array.
  38.        exist a prefix and suffix same ie. "abc"
  39.        *ALGO FOR TEMPORARAY ARRAY:
  40.        1)take and array of length of substring and initially put j at 0 and i at 1,also arr[0]=0;
  41.        2)start comparing jth and ith character,if substring[j]==substring[i],make arr[i]=j+1;
  42.        3)if not matches,then put make j at arr[j-1] abd again compare substring[j] with substring[i] and do
  43.        step 2 again.
  44.      * Compute temporary array to maintain size of suffix which is same as prefix
  45.      * Time/space complexity is O(size of pattern)
  46.      */
  47.     private int[] computeTemporaryArray(char pattern[]){
  48.         int [] lps = new int[pattern.length];
  49.         int index =0;
  50.         for(int i=1; i < pattern.length;){
  51.             if(pattern[i] == pattern[index]){
  52.                 lps[i] = index + 1;
  53.                 index++;
  54.                 i++;
  55.             }else{
  56.                 if(index != 0){
  57.                     index = lps[index-1];
  58.                 }else{
  59.                     lps[i] =0;
  60.                     i++;
  61.                 }
  62.             }
  63.         }
  64.         return lps;
  65.     }
  66.    
  67.     /**
  68.      * KMP algorithm of pattern matching.
  69.      */
  70.     public boolean KMP(char []text, char []pattern){
  71.        
  72.         int lps[] = computeTemporaryArray(pattern);
  73.         int i=0;
  74.         int j=0;
  75.         while(i < text.length && j < pattern.length){
  76.             if(text[i] == pattern[j]){
  77.                 i++;
  78.                 j++;
  79.             }else{          //we are not going back to string,we have got the prefix positon mathching
  80.                 if(j!=0){   //with suffix in substring.and making out comparing position in substring
  81.                     j = lps[j-1];  //equal to end of the prefix using temporary array.
  82.                 }else{
  83.                     i++;
  84.                 }
  85.             }
  86.         }
  87.         if(j == pattern.length){
  88.             return true;
  89.         }
  90.         return false;
  91.     }
  92.        
  93.     public static void main(String args[]){
  94.        
  95.         String str = "abcxabcdabcdabcy";
  96.         String subString = "abcdabcy";
  97.         SubstringSearch ss = new SubstringSearch();
  98.         boolean result = ss.KMP(str.toCharArray(), subString.toCharArray());
  99.         System.out.print(result);
  100.        
  101.     }
  102. }
Add Comment
Please, Sign In to add comment