aboutsummaryrefslogtreecommitdiff
path: root/idx_first_occur_str.cpp
blob: 0f244574dfcfd603518d911a764de18b7c13734e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 28. Find the Index of the First Occurrence in a String
#include "leetcode.h"
#include <vector>

/*
 * given two strings 'needle' and 'haystack', return index of the first
 * occurrence of 'needle' in 'haystack', or -1 if 'needle' is not part of
 * 'haystack'
 */

class Solution {
public:
  int strStr(string haystack, string needle) {
    int m = haystack.size(), n = needle.size();
    if (!n)
      return 0;
    vector<int> lps = kmp(needle);
    for (int i = 0, j = 0; i < m;) {
      if (haystack[i] == needle[j])
        i++, j++;
      if (j == n)
        return i - j;
      if (i < m && haystack[i] != needle[j])
        j ? j = lps[j - 1] : i++;
    }
    return -1;
  }

private:
  // kmp algorithm:
  // http://jakeboxer.com/blog/2009/12/13/the-knuth-morris-pratt-algorithm-in-my-own-words/
  vector<int> kmp(string needle) {
    int n = needle.size();
    vector<int> lps(n, 0);
    for (int i = 1, len = 0; i < n;) {
      if (needle[i] == needle[len])
        lps[i++] = ++len;
      else if (len)
        len = lps[len - 1];
      else
        lps[i++] = 0;
    }
    return lps;
  }
};

int main() {
  Solution obj;
  cout << obj.strStr("sadbutsad", "sad") << endl;  // expect: 0
  cout << obj.strStr("leetcode", "leeto") << endl; // expect: -1
}