LeetCode #28 Find the Index of the First Occurrence in a String Solution & Explanation

Hmm, if find return index else -1, that seems familiar. Included C#, Java ,Python3, JavaScript different solutions with explanation

LeetCode Problem

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

LeetCode #28 Examples and Constraints

Solution

The question seems easy to understand, let’s have a try.

If needle did not occur in haystack then return -1?

Hmm, that sounds familiar.

C# Solution

Solution1

public class Solution {
    public int StrStr(string haystack, string needle) {
        for(int i =0;i<haystack.Length-needle.Length+1;i++)
        {
            if(haystack.Substring(i,needle.Length) == needle)
            {
                return i;
             }
        }
        return -1;
    }
}

Runtime : 106ms、56ms、82ms

Solution2 – IndexOf

public class Solution {
    public int StrStr(string haystack, string needle) {
        return  haystack.IndexOf(needle); 
    }
}

Why return -1 seems so familiar, that’s because the function is 100% same as built-in IndexOf function 🤣

Java Solution

Solution1

class Solution {
    public int strStr(String haystack, String needle) {
        for(int i =0;i<haystack.length()-needle.length()+1;i++)
        {
            if(haystack.substring(i,i+needle.length()).equals(needle))
            {
                return i;
             }
        }
        return -1;
    }
}

C#’s SubString have a little different to Java’s substring

C# : Substring( Start index, Length) vs Java : substring( Start index, End index )

Runtime : 2ms、3ms、1ms

Solution2

class Solution {
    public int strStr(String haystack, String needle) {
        return  haystack.indexOf(needle);
    }
}

Runtime : 0ms、0ms、0ms

Python3 Solution

Solution1

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        hl = len(haystack)
        nl = len(needle)
        
        for i in range(0,hl-nl+1):
            if haystack[i:i+nl] == needle:
                return i
        return -1;

Solution2

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        return haystack.find(needle)
python3 built-in find

JavaScript Solution

Solution1

/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
        for(let i =0;i<haystack.length-needle.length+1;i++)
        {
            if(haystack.substr(i,needle.length) == needle)
            {
                return i;
             }
        }
        return -1;
};

Or you can use substring either, but the parameters need to change

⭐Difference: substr( Start index, Length ) vs substring( Start index, End index )

So, the code will be written as ➡ if(haystack.substring(i,i+needle.length) == needle)

Solution2

/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function(haystack, needle) {
     return  haystack.indexOf(needle); 
};

Conclusion

🧡If my solution helps, that is my honor!

If you got any problem about the explanation or you need other programming language solution, please feel free to let me know (either leave a comment or contact me by Messenger is ok!)

The problem link : Find the Index of the First Occurrence in a String – LeetCode

Reference

Latest Post

Leave a Reply

Your email address will not be published. Required fields are marked *