KatInterview

Find the First Near-Match Code With Optional Folding Rules

StringsAuto-graded · 23 test cases
EasyAuto-gradedStringsstringshamming-distancenormalization

Last updated

Scan candidate codes in order and return the first whose normalized form is within a mismatch limit of a reference, using optional case folding and O/0, I/1 merging.

You are given a reference code and a list of candidate codes, all strings of letters and digits. Return the index of the first candidate that matches the reference under the rules below, or -1 if none does.

Normalization

Before any comparison, the reference and every candidate go through the same normalization, in this order:

  1. Case handling. If case_sensitive is True, letters are compared exactly as given. If it is False, every string is converted to lower case.
  2. Lookalike merging. If merge_lookalikes is True, then o and O become 0, and i and I become 1. This applies whatever the value of case_sensitive. No other characters change. In particular, l and L are not merged with 1.

Matching

After normalization, a candidate matches if both of these hold:

  • It has the same length as the normalized reference. A candidate of a different length never matches.
  • Its mismatch count is at most max_mismatches. The mismatch count is the number of positions where the two strings have different characters.

With max_mismatches = 0, a match is an exact equality of the normalized strings.

Selection

Scan the candidates from left to right and return the index of the first one that matches. Do not look for the candidate with the smallest mismatch count. The earliest acceptable candidate wins, even if a later one is closer.

If max_mismatches is negative, nothing can match, so return -1. If there are no candidates, return -1.

Defaults

max_mismatches defaults to 0, case_sensitive defaults to True, and merge_lookalikes defaults to False.

Constraints

  • Every string consists of ASCII letters and digits.
  • Strings may have any length, including different lengths from the reference.
  • candidates may be empty.
  • max_mismatches is an integer and may be negative.
  • The return value is an integer index into candidates, or -1.

Examples

Example 1

Input: first_close_code('A7K0P91', ['A7K0P91', 'A7K0P92', 'A7KOP91', 'a7k0p91'])

Output: 0

Example 2

Input: first_close_code('A7K0P91', ['x7k0p91', 'A7KOP91', 'A7K0P92', 'ZZZZZZZ'], 0, True, True)

Output: 1

Example 3

Input: first_close_code('A7K0P91', ['a7k0p92', 'A7K0P99', 'B7K0P91', 'A7K0P90'], 1, False, False)

Output: 0

Example 4

Input: first_close_code('O1A2B3C', ['01A2B3C', 'O1A2B3C', 'OIA2B3C', 'x'], 0, True, True)

Output: 0

Hints

Hint 1

Write one helper that applies the normalization steps to a string, and run it on the reference and on every candidate.

Hint 2

Skip any candidate whose normalized length differs from the reference. Otherwise count the positions where the characters differ.

Hint 3

Return as soon as a candidate has a mismatch count within the limit. Do not keep searching for a closer one. Remember the negative-limit case.