There's a couple of logic errors in your code.
The problem happens here:
while i < len(x) and i != j:
i += 1
res.append(x[i])
You keep increasing i
until it is either len(x)
or greater, or until it is the same as j
. But since you set j
to be 2
at the start (and never update it), it will simply end up setting i
to len(x)
. And x[i]
will thus fail, since x[len(x)]
tries to index an element just outside x
.
However, there's a few more remarks to make:
- you collect what you find in
res
, but really only want a number (e.g. 2
) as a result
- you define
count
but don't use it
- you track the coordinates in the string in three separate variables (
i
, j
, k
) and have a lot of logic to increment the first, but really all you need is to step through the string one position at a time, and look at the offsets directly
Given all that and the problem description, you were probably going for something like this:
x = "aabbcc"
y = "abc"
def solution(x, y):
i, count = 0, 0
while i + 4 < len(x):
if (x[i], x[i+2], x[i+4]) == (y[0], y[1], y[2]):
count += 1
i += 1
return count
print(solution(x, y))
However, Python has some cleverness that would make it even simpler (or at least shorter):
def solution(x, y):
count = 0
for i in range(len(x)-4):
if x[i:i+5:2] == y: # slicing with a stride of two, instead of direct indexing
count += 1
return count
Or even:
def solution(x, y):
return len([x for i in range(len(x)-4) if x[i:i+5:2] == y])
But that's favouring brevity over readability a bit too much, I feel.