Sunday, April 19, 2015

Implement strStr()

Problem:
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Update (2014-11-02):
The signature of the function had been updated to return the index instead of the pointer. If you still see your function signature returns a char * or String, please click the reload button  to reset your code definition.


Solution:
class Solution {
public:
    int strStr(string haystack, string needle) {
        int sol = - 1;
        int size = haystack.size(), size_ndl = needle.size();
        if (size_ndl > size)
            return sol;
        else if (size_ndl == 0)
            return 0;
            
        for (int i = 0; i < (size - size_ndl + 1); i++) {
            if (haystack[i] == needle[0]) {
                for (int j = 0; j < size_ndl; j++) {
                    if (haystack[i + j] != needle[j])
                        break;
                    else if (j == (size_ndl - 1) && haystack[i + j] == needle[j])
                        return i;
                }
            }
        }
        return sol;
    }
};

==== Second time ====
 bool check_need(string haystack, int index, string needle) {
            int j = 0;
            int need_size = needle.size();
         
            if (haystack.size() - index < need_size) {
                return false;
            }
            for (int i = index; i < haystack.size(); i++) {
                if (j == needle.size()) {
                    return true;
                }
                if (haystack[i] != needle[j++]) {
                    return false;
                }
            }
            return true;
        }
 
    int strStr(string haystack, string needle) {
        int need_size = needle.size();
        int hay_size = haystack.size();
        int ans = 0;
        if (hay_size < need_size) {
            return -1;
        }
     
        for (int i = 0; i < hay_size; i++) {
            if (haystack[i] == needle[0] &&
                check_need(haystack, i, needle)) {
                return i;
            }
        }
     
        if (need_size == 0) {
            return 0;
        } else {
            return -1;
        }
    }
=========
class Solution {
public:
    int strStr(string haystack, string needle) {
        if (needle == haystack || needle.empty()) {
            return 0;
        }
        if (haystack.empty() || needle.size() > haystack.size()) {
            return -1;
        }

        for (int i = 0; i <= haystack.size() - needle.size(); i++) {
            if (haystack[i] == needle[0] && haystack.substr(i, needle.size()) == needle) {
                return i;
            }
        }
        return -1;
    }
};
====================
class Solution {
public:
    int strStr(string haystack, string needle) {
        int ans = 0;
        if (needle.size() == 0) {
            return ans;
        }
        if (haystack.size() == 0 || needle.size() > haystack.size()) {
            return -1;
        }
        
        int iter = 0, checker = 0;
        while (iter <= haystack.size() - needle.size()) {
            int tmp = iter;
            while (checker < needle.size() &&
                   haystack[tmp + checker] == needle[checker]) {
                checker++;
            }
            if (checker == needle.size()) {
                return iter;
            }
            iter++;
            checker = 0;
        }
        return -1;
        
    }
};

No comments:

Post a Comment