Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.interview.string;
- // * Runtime complexity - O(m + n) where m is length of text and n is length of pattern
- // * Space complexity - O(n)
- public class SubstringSearch {
- //* Slow method of pattern matching Time complexity O(m*n)
- public boolean hasSubstring(char[] text, char[] pattern){
- int i=0;
- int j=0;
- int k = 0;
- while(i < text.length && j < pattern.length){
- if(text[i] == pattern[j]){
- i++;
- j++;
- }else{
- j=0;
- k++;
- i = k;
- }
- }
- if(j == pattern.length){
- return true;
- }
- return false;
- }
- /** 012345678910 11 01234567
- * eg. string abcdabcdab c y substring:abcdabcy
- * start comparing both string from 0index,at index 7,string doesn't matches so comparing again from index 1 of string
- and index 0 of string,is normal substring matching algo but we will search of prefix and suffix till index 6 in
- substring and there exist a prefix and suffix in substring till index6,ie."abc" so we will not got back to 0th
- position in substring,we will go to index4 in because before index4 element,position 3 to 6 will already have the
- substring because, "abc" is suffix also.
- *for storing the prefix-suffix match,we will make an temporary array.
- exist a prefix and suffix same ie. "abc"
- *ALGO FOR TEMPORARAY ARRAY:
- 1)take and array of length of substring and initially put j at 0 and i at 1,also arr[0]=0;
- 2)start comparing jth and ith character,if substring[j]==substring[i],make arr[i]=j+1;
- 3)if not matches,then put make j at arr[j-1] abd again compare substring[j] with substring[i] and do
- step 2 again.
- * Compute temporary array to maintain size of suffix which is same as prefix
- * Time/space complexity is O(size of pattern)
- */
- private int[] computeTemporaryArray(char pattern[]){
- int [] lps = new int[pattern.length];
- int index =0;
- for(int i=1; i < pattern.length;){
- if(pattern[i] == pattern[index]){
- lps[i] = index + 1;
- index++;
- i++;
- }else{
- if(index != 0){
- index = lps[index-1];
- }else{
- lps[i] =0;
- i++;
- }
- }
- }
- return lps;
- }
- /**
- * KMP algorithm of pattern matching.
- */
- public boolean KMP(char []text, char []pattern){
- int lps[] = computeTemporaryArray(pattern);
- int i=0;
- int j=0;
- while(i < text.length && j < pattern.length){
- if(text[i] == pattern[j]){
- i++;
- j++;
- }else{ //we are not going back to string,we have got the prefix positon mathching
- if(j!=0){ //with suffix in substring.and making out comparing position in substring
- j = lps[j-1]; //equal to end of the prefix using temporary array.
- }else{
- i++;
- }
- }
- }
- if(j == pattern.length){
- return true;
- }
- return false;
- }
- public static void main(String args[]){
- String str = "abcxabcdabcdabcy";
- String subString = "abcdabcy";
- SubstringSearch ss = new SubstringSearch();
- boolean result = ss.KMP(str.toCharArray(), subString.toCharArray());
- System.out.print(result);
- }
- }
Add Comment
Please, Sign In to add comment