/* * @lc app=leetcode id=28 lang=java * * [28] Find the Index of the First Occurrence in a String */ // @lc code=start class Solution { public int strStr(String haystack, String needle) { int idx = -1; if(haystack.length() < needle.length()) { return idx; } for(int i = 0; i < haystack.length(); i++) { if(i + needle.length() <= haystack.length() && haystack.substring(i, i + needle.length()).equals(needle)) { idx = i; break; } } return idx; } } // @lc code=end