A match that starts near the front of a candidate usually beats one that starts deep inside it. Today you add a small, capped penalty for characters skipped before the first match.
Subtract a penalty for each candidate character skipped before the first matched character, capped at a small maximum.
Where a match begins carries information. Type main and the copy that starts at the front of main.go is usually what you want, more than the main buried inside src/domain/main. The leading-gap penalty captures that: each candidate character skipped before the first matched character costs -1, so earlier starts score higher.
The important design choice is the cap. Without one, a match deep in a long path would be penalized into oblivion and could never rank, even when it is the only thing that matches. Capping the leading penalty at -3 keeps a mild preference for early matches while still letting a strong deep match compete. This is the model’s one asymmetry with the between-match gap penalty from lesson 7, which is uncapped - a reminder that leading gaps and interior gaps play different roles.
// The FIRST matched char (k == 0) has a leading gap equal to its index.// Penalize it, but cap the penalty so a deep match is not crushed.const maxLeading = 3// inside the k == 0 branch:lead := p // p characters were skipped before the first matchif lead > maxLeading { lead = maxLeading }s -= lead