Sunday, July 2, 2023

Simple Java Solution for the Leetcode question : 28. Find the Index of the First Occurrence in a String

Java Solution for Leetcode question 28. Find the Index of the First Occurrence in a String

Question : 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.



Solution : We are going to use indexOf()  method to solve this problem. 
  • indexOf(String str) method : 
    Returns the index within this string of the first occurrence of the specified substring.

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



    It beasts 100%.


Happy Coding :)


Check our blog here : https://surajkaschool.blogspot.com/ 



If you have any query or suggestion please mention them in the comment box. Also feel free to Connect with us on: Facebook: https://www.facebook.com/SurajKaSchool/  -------------------------------------------------------------------------------------
Let's have a discussion on Facebook group: To connect with us please click here: https://bit.ly/TechnosEducationGroup 
 If you like this post kindly comment below and do share your response. Thanks for watching.

No comments:

Post a Comment

Google Account rename kaise karen II How to rename Google Account II Google Account II Google

In this video you can see how to rename your Google Account and its nick name. If you have any query or suggestion please mention them in th...