You are given several independent implementation-focused tasks. For each task, write a function that returns the required value.
Task 1 — Count matching 3-character windows (case-insensitive)
Given a string s, consider every contiguous substring of length 3: s[i..i+2] for 0 ≤ i ≤ len(s)-3.
Count how many such windows satisfy:
-
lower(s[i]) == lower(s[i+2])
(case-insensitive comparison)
Output: an integer count.
Notes:
-
If
len(s) < 3
, the answer is
0
.
Task 2 — Grid “match-3” explosion with gravity
You are given an m x n integer matrix grid where each integer represents a “color”.
A cell (r, c) has up to 4 orthogonal neighbors: up, down, left, right.
Explosion rule
A cell (r, c) is considered a trigger if at least 2 of its orthogonal neighbors have the same value as grid[r][c].
When a cell triggers, the cell itself and all orthogonal neighbors that match its value are marked to be removed.
All removals for the step happen simultaneously.
Gravity rule
After removals, the removed positions become empty. Then, for each column independently:
-
All remaining values in the column “fall” downward (preserving their relative order).
-
Empty cells are filled at the top with
0
.
Iteration
Repeat Explosion → Gravity until no more cells trigger.
Output: the final matrix after the process stabilizes.
Clarifications:
-
0
is treated as an empty cell after gravity; it does
not
participate in explosions.
Task 3 — Count ordered pairs that form a target string
Given a list of strings words of length n and a target string target, count the number of ordered index pairs (i, j) such that:
-
0 ≤ i, j < n
-
i != j
-
words[i] + words[j] == target
Output: an integer count.
Important: Ordered pairs are distinct, so (i, j) and (j, i) are counted separately when both satisfy the condition.