Arena / Problems /

28. Find the Index of the First Occurrence in a String

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #28

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.

Example 1
Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Explanation:

"sad" occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.

Example 2
Input: haystack = "leetcode", needle = "leeto"
Output: -1
Explanation:

"leeto" did not occur in "leetcode", so we return -1.

Constraints
  • 1 <= haystack.length, needle.length <= 10000
  • haystack and needle consist of only lowercase English characters.
Topics & Algorithms
⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:
  • This kind of question can be solved with one line of code using the built-in index(). Obviously, the questioner wants to test our ability to control the loop.

  • For heystack, iterate through each character in turn. If the substring from the current character to needle is the same as needle, return the position of the current character.

  • This question is easier to understand by looking at the code directly.

class Solution:
    def strStr(self, haystack: str, needle: str) -> int:
        n = len(haystack)
        m = len(needle)

        for i in range(n - m + 1):
            if haystack[i:i + m] == needle:
                return i

        return -1
⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Javascript Solution by Anonymous Developer
0
Approach Intuition:
  • This kind of question can be solved with one line of code using the built-in index(). Obviously, the questioner wants to test our ability to control the loop.

  • For heystack, iterate through each character in turn. If the substring from the current character to needle is the same as needle, return the position of the current character.

  • This question is easier to understand by looking at the code directly.

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

  for (let i = 0; i <= n - m; i++) {
    if (haystack.substring(i, i + m) === needle) {
      return i;
    }
  }

  return -1;
};

⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:
  • This kind of question can be solved with one line of code using the built-in index(). Obviously, the questioner wants to test our ability to control the loop.

  • For heystack, iterate through each character in turn. If the substring from the current character to needle is the same as needle, return the position of the current character.

  • This question is easier to understand by looking at the code directly.

# @param {String} haystack
# @param {String} needle
# @return {Integer}
def str_str(haystack, needle)
  (0..haystack.size - needle.size).each do |i|
    if haystack[i...i + needle.size] == needle
      return i  
    end
  end

  -1
end
⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C++ Solution by Anonymous Developer
0
Approach Intuition:
  • This kind of question can be solved with one line of code using the built-in index(). Obviously, the questioner wants to test our ability to control the loop.

  • For heystack, iterate through each character in turn. If the substring from the current character to needle is the same as needle, return the position of the current character.

  • This question is easier to understand by looking at the code directly.

class Solution {
public:
    int strStr(string haystack, string needle) {
        for (int i = 0; i < haystack.length(); i++) {
            int j = 0;

            while (i + j < haystack.length() && haystack[i + j] == needle[j]) {
                j++;

                if (j == needle.length()) {
                    return i;
                }
            }
        }

        return -1;
    }
};
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:
  • This kind of question can be solved with one line of code using the built-in index(). Obviously, the questioner wants to test our ability to control the loop.

  • For heystack, iterate through each character in turn. If the substring from the current character to needle is the same as needle, return the position of the current character.

  • This question is easier to understand by looking at the code directly.

class Solution {
    public int strStr(String haystack, String needle) {
        for (int i = 0; i < haystack.length(); i++) {
            int j = 0;

            while (i + j < haystack.length() && haystack.charAt(i + j) == needle.charAt(j)) {
                j++;

                if (j == needle.length()) {
                    return i;
                }
            }
        }

        return -1;
    }
}
⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Go Solution by Anonymous Developer
0
Approach Intuition:
  • This kind of question can be solved with one line of code using the built-in index(). Obviously, the questioner wants to test our ability to control the loop.

  • For heystack, iterate through each character in turn. If the substring from the current character to needle is the same as needle, return the position of the current character.

  • This question is easier to understand by looking at the code directly.

func strStr(haystack string, needle string) int {
    for i := 0; i < len(haystack); i++ {
        j := 0

        for i+j < len(haystack) && haystack[i+j] == needle[j] {
            j++

            if j == len(needle) {
                return i
            }
        }
    }

    return -1
}
⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C# Solution by Anonymous Developer
0
Approach Intuition:
  • This kind of question can be solved with one line of code using the built-in index(). Obviously, the questioner wants to test our ability to control the loop.

  • For heystack, iterate through each character in turn. If the substring from the current character to needle is the same as needle, return the position of the current character.

  • This question is easier to understand by looking at the code directly.

public class Solution {
    public int StrStr(string haystack, string needle) {
        for (int i = 0; i < haystack.Length; i++) {
            int j = 0;

            while (i + j < haystack.Length && haystack[i + j] == needle[j]) {
                j++;

                if (j == needle.Length) {
                    return i;
                }
            }
        }

        return -1;
    }
}
⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #28 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.