I am working on the follow up question to isSubsequence from leetcode and need to know what the time complexity for my solution is. I am just starting to figure big-O out and would love some help on figuring out what it is for this one and how I can begin to understand why it is what it is. I believe this is O(n log n) but couldn't explain why other than the fact that I use a binary search
const s = 'a';
const t = 'abc';
function populateHashTable(t) {
const letterToIndexHashTable = {}; // {a: [0], b: [1], c: [2]}
for (let i = 0; i < t.length; i++) {
const currentLetter = t[i];
if (currentLetter in letterToIndexHashTable) {
letterToIndexHashTable[currentLetter].push(i);
} else {
letterToIndexHashTable[currentLetter] = [i];
}
}
return letterToIndexHashTable;
}
const letterToIndexHashTable = populateHashTable(t); // {a: [0], b: [1], c: [2]}
function isSubsequence(s) {
let previousIndex = 0;
let lettersMatched = 0;
for (let i = 0; i < s.length; i++) {
const currentLetter = s[i];
if (currentLetter in letterToIndexHashTable) {
const nextLetterInSubsequence = letterToIndexHashTable[currentLetter]
.find(element => element >= previousIndex);
if (nextLetterInSubsequence >= 0) {
previousIndex = nextLetterInSubsequence;
lettersMatched++;
} else {
return false;
}
} else {
return false;
}
}
if (lettersMatched === s.length) {
return true;
} else {
return false;
}
}