A 0ms C solution


  • 0

    Used the idea of KMP algorithm.

    int strStr(char* hs, char* ne) 
    {
        if (!strlen(hs) && !strlen(ne))
    		return 0;
    	if (!hs || !ne )
    		return -1;
    	if (strlen(hs) < strlen(ne))
    		return -1;
    	if (!strlen(ne))
    		return 0;
    	int p = 0;
    	int cur=0;
    	int op=0;
    	int pos=-1;
    	int flag=1;
    	for (cur=0; hs[cur];)
    	{
    		if (hs[cur] == ne[0])
    		{
    			flag = 1;
    			op = cur;
    			p = 0;
    			pos = -1;
    			for (; ne[p];++p,++op)
    			{
    				if (!hs[op])
    					return -1;
    				if (ne[0] == hs[op] && op != cur && pos == -1)
    					pos = op; 
    				if (ne[p] != hs[op])
    				{
    					flag = 0;
    					break;
    				}
    			}
    			if (flag)
    				return cur;
    			else
    			{
    				if (pos != -1)
    					cur = pos;
    				else
    					++cur;
    			}
    		}
    		else
    			++cur;
    	}
    	return -1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.