function sunday(main, pattern) {
const patternLastMap = new Map();
const sLen = main.length,
tLen = pattern.length;
for (let i = 0; i < tLen; i++) {
patternLastMap.set(pattern[i], i);
}
let curIndex = 0;
while (curIndex <= sLen - tLen) {
let j = 0;
while (j < tLen && main[curIndex + j] === pattern[j]) {
j++;
}
if (j === tLen) {
return curIndex;
} else {
const next = curIndex + tLen,
latsStrIndex = patternLastMap.get(main[next]);
if (latsStrIndex > -1) {
curIndex = next - latsStrIndex;
} else {
curIndex = next + 1;
}
}
}
return -1;
}
console.log(sunday("ABABC", "ABC"));