Find the Index of the First Occurrence in a String

PHOTO EMBED

Thu Sep 29 2022 12:27:44 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
   int strStr(string s1,string s2)
   {
   int l1=s1.length();
   int l2=s2.length();
   if(s1==s2) return 0;
   if(l1<l2) return -1;
   for(int i=0;i<=l1-l2;i++)
   {
      int j=0;
      while(j<l2)
      {
         if(s1[i]==s2[j])
         {
            i++;
            j++;
         }
         else break;
      }
      if(j==l2) return i-j;
      else i=i-j;
   }
   return -1;
   }
};
content_copyCOPY

https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/