Skip to content

Commit

Permalink
KMP algorithm implementation for string search
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp authored and Shubham Patil committed Oct 16, 2019
1 parent 17e556b commit 5461319
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ LeetCode
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium|
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy|
|27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy|
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [C](./src/28.c)|Easy|
|35|[Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [C](./src/35.c)|Easy|
|53|[Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [C](./src/53.c)|Easy|
|82|[Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/) | [C](./src/82.c)|Medium|
Expand Down Expand Up @@ -73,4 +74,4 @@ LeetCode
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [C](./src/1089.c)|Easy|
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c)|Easy|
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy|
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy|
104 changes: 104 additions & 0 deletions leetcode/src/28.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* brute force approach
* time complexity: O(mn)
*/
int strStr(char* haystack, char* needle) {
int i = 0;
int j = 0;
int k = 0;
int hlen = 0;
int nlen = 0;

if(needle == NULL || *needle == 0)
return 0;

if(haystack == NULL || *haystack == 0)
return -1;

hlen = strlen(haystack);
nlen = strlen(needle);

if(hlen < nlen)
return -1;

for(i = 0; i <= hlen - nlen; i++) {
j = 0;
if(haystack[i] != needle[j++])
continue;

k = i + 1;
for(; j < nlen; j++) {
if(haystack[k] != needle[j]) {
break;
} else
k++;
}
if(j == nlen)
return i;
}
return -1;
}

/* ---------------------------------------------------------------------------------------- */

/*
* KMP algorithm
* time complexity: O(m + n)
*/

/* fills overlap with longest proper prefix which is also suffix for each index in needle */
void fill_overlap(char *needle, int len_needle, int *overlap)
{
int len = 0;
int i = 0;

overlap[0] = 0;

for (i = 1; i < len_needle;) {
if (needle[i] == needle[len]) {
len++;
overlap[i++] = len;
} else {
if (len)
len = overlap[len - 1];
else
overlap[i++] = 0;
}
}
}

int strStr(char *haystack, char *needle)
{
int i = 0; /* index for haystack */
int j = 0; /* index for needle */

int len_needle = strlen(needle);
int len_haystack = strlen(haystack);

if (!len_needle)
return 0;

int overlap[len_needle];

fill_overlap(needle, len_needle, overlap);

while (i < len_haystack) {
if (needle[j] == haystack[i]) {
i++;
j++;
}

if (j == len_needle) {
return (i - j);
} else if (i < len_haystack && needle[j] != haystack[i]) {
if (j != 0)
j = overlap[j - 1];
else
i = i + 1;
}
}
return -1;
}

/* ---------------------------------------------------------------------------------------- */

0 comments on commit 5461319

Please sign in to comment.