/*
 * @lc app=leetcode.cn id=28 lang=javascript
 *
 * [28] 找出字符串中第一个匹配项的下标
 */

// @lc code=start
/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */
var strStr = function (haystack, needle) {
  const len1 = haystack.length;
  const len2 = needle.length;
  if (len1 < len2) return -1;
  if (len2 === 0) return 0;

  for (let i = 0; i <= len1 - len2; i++) {
    for(let j = 0; j < len2; j++) {
      if(haystack[i + j] !== needle[j]) {
        break; // 如果不匹配,跳出内层循环
      }
      if(j === len2 - 1) {
        return i; // 如果内层循环完成,说明找到了匹配项
      }
    }

  }
  return -1; // 没有找到匹配项
};
// @lc code=end

console.log(strStr("hello", "ll")); // 2
console.log(strStr('sadbutsad', 'sad')); // 0
console.log(strStr('leetcode', 'leeto')); // -1