So I have this code here and I am just trying to understand the time and space complexity.
for time complexity I think its O(n^2) because it is going through at most n - 1 loops in the while loop and it will go through n times in the for loop so it will be O(n(n-1)) which is O(n^2) and space complexity I think its O(n) because it is linear space.
I don't know if I'm right but if I am wrong can someone correct my thinking? Thanks in advance.
// Write your code here
let visited = new Array(s.length).fill(false);
let count = 0;
for (let i = 0; i < s.length; i++) {
let j = i + 1;
visited[i] = true;
while (j < s.length && s[j] === s[i] && !visited[j]) {
visited[j] = true;
count++;
j++;
}
}
return count;