KatInterview

Widest Alphabetic Boundary Gap Across Tokens

StringsAuto-graded · 22 test cases
MediumAuto-gradedStringsstringsscanningcase-normalization

Last updated

Tests string scanning, ASCII letter detection, and single-pass maximum tracking with tie handling across whitespace-delimited tokens.

Implement a function max_alpha_gap(text: str) that receives a single text string and reports the widest alphabetic boundary gap found in any of its words, together with every word that attains that gap.

Tokenization. A word is a maximal run of non-whitespace characters, exactly as produced by splitting the text on whitespace (text.split()). Punctuation, digits, and other symbols never break a token: can't and well-known are each one word, and (x) is one word.

Letters. Only the ASCII letters a–z and A–Z count as letters. Any other character (digits, punctuation, and non-ASCII letters such as é) is ignored when locating boundaries.

Gap of a word. Look at the first ASCII letter of the token and the last ASCII letter of the token, ignoring all non-letters in between and at the ends. Map each of those two letters to its position in a=0, b=1, ..., z=25 (case is irrelevant) and take the absolute difference. A word whose first and last letters coincide has gap 0. The direction does not matter: wood (w→d) and dew (d→w) both have gap 19.

Result. Return a tuple (max_gap, words) where:

  • max_gap is the largest gap over all tokens that contain at least one ASCII letter;
  • words is the list of tokens achieving max_gap, listed in the order they appear in the text, keeping their original surface form — capitalization and any punctuation are preserved exactly as they occur (for example, cannot. is returned as cannot., and Can't! is returned as Can't!);
  • every occurrence is listed, so a token that appears more than once appears in words more than once.

Empty case. If the text contains no token with an ASCII letter (for example "", "123 !!! --"), return (0, []).

Constraints

  • text may be empty and may contain any printable characters, tabs, and newlines.
  • The length of text is at most 100,000 characters.
  • The result list may contain up to O(n) tokens, where n is the length of text.

Examples

Example 1

Input: max_alpha_gap('cab pat Float wood would I pop')

Output: [19, ['wood', 'would']]

Example 2

Input: max_alpha_gap('can\'t cannot.')

Output: [17, ['can\'t', 'cannot.']]

Example 3

Input: max_alpha_gap('The quick brown fox jumps over the lazy dog.')

Output: [18, ['fox']]

Example 4

Input: max_alpha_gap('123 !!! --')

Output: [0, []]

Hints

Hint 1

A word's gap depends only on its first and last ASCII letters; everything in between is irrelevant.

Hint 2

Use `str.split()` for whitespace tokenization and scan each token character by character to locate its two boundary letters.

Hint 3

Keep a running maximum: on a strictly larger gap replace the winners list, on an equal gap append the token (duplicates included).

Hint 4

Case is irrelevant for the gap computation, but the returned token must keep its original surface form.