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/
Instagram: https://www.instagram.com/surajkaschool/
Twitter: https://twitter.com/SurajKaSchool
LinkedIn : https://linkedin.com/company/suraj-ka-school
YouTube: https://bit.ly/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.
