Advertisement
1988coder

28. Implement strStr()

Jan 4th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. /**
  2.  * Brute Force string matching.
  3.  *
  4.  * Time Complexity: O(M * N)
  5.  *
  6.  * Space Complexity: O(1)
  7.  *
  8.  * M = Length of haystack string. N = length of fneedle string.
  9.  */
  10. class Solution1 {
  11.     public int strStr(String haystack, String needle) {
  12.         if (needle.isEmpty()) {
  13.             return 0;
  14.         }
  15.         for (int i = 0; i <= haystack.length() - needle.length(); i++) {
  16.             for (int j = 0; j < needle.length(); j++) {
  17.                 if (haystack.charAt(i + j) != needle.charAt(j)) {
  18.                     break;
  19.                 }
  20.                 if (j == needle.length() - 1) {
  21.                     return i;
  22.                 }
  23.             }
  24.         }
  25.         return -1;
  26.     }
  27. }
  28.  
  29. /**
  30.  * Using KMP Algorithm
  31.  *
  32.  * Time Complexity: O(M + N). O(N) to create lookup table. O(M) to find the
  33.  * needle in haystack.
  34.  *
  35.  * Space Complexity: O(N). This is required to save the lookup table.
  36.  *
  37.  * M = Length of haystack string. N = length of fneedle string.
  38.  */
  39. class Solution2 {
  40.     public int strStr(String haystack, String needle) {
  41.         if (haystack == null || needle == null) {
  42.             return -1;
  43.         }
  44.         if (needle.length() == 0) {
  45.             return 0;
  46.         }
  47.         if (haystack.length() == 0) {
  48.             return -1;
  49.         }
  50.  
  51.         int[] table = kmpLookupTable(needle);
  52.  
  53.         int i = 0;
  54.         int j = 0;
  55.         while (i < haystack.length() && j < needle.length()) {
  56.             if (haystack.charAt(i) == needle.charAt(j)) {
  57.                 i++;
  58.                 j++;
  59.             } else {
  60.                 if (j > 0) {
  61.                     j = table[j - 1];
  62.                 } else {
  63.                     i++;
  64.                 }
  65.             }
  66.         }
  67.         if (j == needle.length()) {
  68.             return i - j;
  69.         } else {
  70.             return -1;
  71.         }
  72.     }
  73.  
  74.     private int[] kmpLookupTable(String s) {
  75.         int[] table = new int[s.length()];
  76.         int i = 1;
  77.         int index = 0;
  78.         while (i < s.length()) {
  79.             if (s.charAt(i) == s.charAt(index)) {
  80.                 table[i] = index + 1;
  81.                 index++;
  82.                 i++;
  83.             } else {
  84.                 if (index > 0) {
  85.                     index = table[index - 1];
  86.                 } else {
  87.                     table[i] = 0;
  88.                     i++;
  89.                 }
  90.             }
  91.         }
  92.         return table;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement