Implement strStr()

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().

Method 1:

        public int StrStr(string h, string n)
        {
            if (string.IsNullOrEmpty(n) || n == h) return 0;
            if (n.Length > h.Length) return -1;

            for (int i = 0; i < h.Length - n.Length + 1; ++i)
            {
                if (h[i] == n[0])
                {
                    for (int j = 0; j < n.Length; j++)
                    {
                        if (h[i + j] != n[j]) break;
                        if (j == n.Length - 1) return i;
                    }
                }
            }

            return -1;
        }

Method 2:

    public int StrStr(string h, string n) {
        if(h == n) return 0;
        else if(String.IsNullOrEmpty(h)) return -1;
        else if(String.IsNullOrEmpty(n)) return 0;
        
                
        for(int i = 0; i<h.Length;++i)
        {   
            int j = 0;
            while(j < n.Length && (i+j) < h. Length && h[i + j] == n[j])
            {
                j++;
            }
            
            if(j == n.Length)
            {
                return i;
            }
        }
        
        return -1;
    }

Program to Insert a substring in a main string.

/* We have a main string and a sub string that has to be inserted somewhere in main string according to user. User insert main string and substring both, as well as he entered index where substring has to be inserted. If we have space in main string and we want to keep the main string in proper format as we insert than we have to add space along with substring as input. */

#include
#include

void insert(char sup[], char sub[], int pos,int supl, int subl)
{int i,j,n;
n=supl+subl;
char temp[1000];
printf(“\nsupl= %d\n”,supl);
printf(“\nsubl= %d\n”,subl);

for(i=0;i
temp[i]=sup[i];

for(j=0;j<subl;j++,i++)
temp[i]=sub[j];

for(;pos=supl || pos<0)
{puts(“\nArray out of bound! You entered invalid index.”);
exit(1);
}
else insert(sup,sub,pos,supl,subl);
}

Program to Insert a substring in a main string.

/* We have a main string and a sub string that has to be inserted somewhere in main string according to user. User insert main string and substring both, as well as he entered index where substring has to be inserted. If we have space in main string and we want to keep the main string in proper format as we insert than we have to add space along with substring as input. */

#include
#include

void insert(char sup[], char sub[], int pos,int supl, int subl)
{int i,j,n;
n=supl+subl;
char temp[1000];
printf(“\nsupl= %d\n”,supl);
printf(“\nsubl= %d\n”,subl);

for(i=0;i<pos;i++)
temp[i]=sup[i];

for(j=0;j<subl;j++,i++)
temp[i]=sub[j];

for(;pos=supl || pos<0)
{puts(“\nArray out of bound! You entered invalid index.”);
exit(1);
}
else insert(sup,sub,pos,supl,subl);
}

Program to insert a substring into main string.

A C program that uses functions to perform the following operations:  To insert a sub-string in to given main string from a given position.

#include <stdio.h>
#include <conio.h>
#include <string.h>

void main()
{
char a[10];
char b[10];
char c[10];
int p=0,r=0,i=0;
int t=0;
int x,g,s,n,o;
clrscr();

puts("Enter First String:");
gets(a);
puts("Enter Second String:");
gets(b);
printf("Enter the position where the item has to be inserted: ");
scanf("%d",&p);
r = strlen(a);
n = strlen(b);
i=0;

// Copying the input string into another array
while(i <= r)
{
c[i]=a[i];
i++;
}
s = n+r;
o = p+n;

// Adding the sub-string
for(i=p;i<s;i++)
{
x = c[i];
if(t<n)
{
a[i] = b[t];
t=t+1;
}
a[o]=x;
o=o+1;
}

printf("%s", a);
getch();
}