FastPrepBitFont - Maximum Non-Overlapping Kerning

BitFont - Maximum Non-Overlapping Kerning

Stripe logoStripe● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

Two selected bitmap glyphs are supplied as leftGlyph and rightGlyph. Each glyph is a rectangular list of binary row strings. A 1 is ink and a 0 is background. Both glyphs have the same height.

Placement rules

Let L and R be the glyph widths. The left glyph starts at column 0. A kerning value k, with 0 <= k <= L, places the right glyph at column L - k. Thus k = 0 puts the glyphs side by side, and a larger value moves the right glyph closer.

A placement is valid when no output cell receives ink from both glyphs. Background columns and even the glyph bounding boxes may overlap. Evaluate each final placement independently: an invalid smaller value does not rule out a larger valid value. The right glyph may start at column 0, but never at a negative column.

Return the largest valid k. The placement at k = 0 always has disjoint glyph extents, so a result always exists.

Do not stop at the first collision while increasing k. For example, with leftGlyph = ["101"] and rightGlyph = ["010"], values 0, 1 and 3 are valid, while 2 collides. The answer is 3.

Function

maximumKerning(leftGlyph: String[], rightGlyph: String[]) → int

Examples

Example 1

leftGlyph = ["101"]rightGlyph = ["010"]return = 3

The largest valid final-placement shift is 3. The right origin is 0. Every larger allowed shift is invalid; smaller collisions do not exclude this final placement.

Example 2

leftGlyph = ["11","11"]rightGlyph = ["1","1"]return = 0

The largest valid final-placement shift is 0. The right origin is 2. Every larger allowed shift is invalid; smaller collisions do not exclude this final placement.

Example 3

leftGlyph = ["0010","0100"]rightGlyph = ["100","001"]return = 4

The largest valid final-placement shift is 4. The right origin is 0. Every larger allowed shift is invalid; smaller collisions do not exclude this final placement.

Constraints

  • 1 <= leftGlyph.length == rightGlyph.length <= 100.
  • Every row in leftGlyph has the same width L; every row in rightGlyph has the same width R.
  • 1 <= L, R <= 100.
  • Rows contain only ASCII 0 and 1. Entirely background rows or glyphs are allowed.

More Stripe problems

See Stripe hiring insights
public int maximumKerning(String[] leftGlyph, String[] rightGlyph) {
    // Write your code here.
}
leftGlyph["101"]
rightGlyph["010"]
expected3
Checking account…