I stumbled upon this excercise:
Create a function that returns the longest substring with unique characters.
and I'm just wondering if I have accomplished O(n) time complexity with the solution I came up with.
i.e:
getMaxSubStr("linkshortener") -> "linkshorte"
there's only one for loop no nesting so this should be O(n) right, what's giving me doubts is that when tempStr stumbles a repeating character the iterator goes back to the string index of where the repeating character is last seen, so that adds to the runtime operations needed to be done and not sure if it's still O(n)
async function maxUniqueStr(string) {
let maxStr = "";
let tempStr = "";
let lastSeen = {};
for (let i = 0; i < string.length; i++) {
let char = string.charAt(i);
if (tempStr.includes(char)) {
i = lastSeen[char];
tempStr = "";
console.log("-continue-", i);
continue;
}
tempStr += char;
maxStr = tempStr.length > maxStr.length ? tempStr : maxStr;
lastSeen[char] = i;
}
return maxStr;
}