Maximum Font to Fit a Sentence in a Screen - Problem

You are given a string text. We want to display text on a screen of width w and height h. You can choose any font size from array fonts, which contains the available font sizes in ascending order.

You can use the FontInfo interface to get the width and height of any character at any available font size. The FontInfo interface is defined as such:

interface FontInfo {
  // Returns the width of character ch on the screen using font size fontSize. O(1) per call
  public int getWidth(int fontSize, char ch);
  // Returns the height of any character on the screen using font size fontSize. O(1) per call
  public int getHeight(int fontSize);
}

The calculated width of text for some fontSize is the sum of every getWidth(fontSize, text[i]) call for each 0 <= i < text.length (0-indexed).

The calculated height of text for some fontSize is getHeight(fontSize).

Note that text is displayed on a single line.

It is guaranteed that FontInfo will return the same value if you call getHeight or getWidth with the same parameters.

It is also guaranteed that for any font size fontSize and any character ch:

  • getHeight(fontSize) <= getHeight(fontSize+1)
  • getWidth(fontSize, ch) <= getWidth(fontSize+1, ch)

Return the maximum font size you can use to display text on the screen. If text cannot fit on the display with any font size, return -1.

Input & Output

Example 1 — Basic Case
$ Input: text = "helloworld", w = 80, h = 20, fonts = [6,8,10,12,14,16,18,24,36]
Output: 6
💡 Note: Font size 6 is the largest that fits within width 80 and height 20 for text 'helloworld'
Example 2 — No Font Fits
$ Input: text = "leetcode", w = 10, h = 5, fonts = [10,12,14,16,18,20]
Output: -1
💡 Note: Even the smallest font (10) is too big for the tiny screen dimensions
Example 3 — Large Screen
$ Input: text = "a", w = 1000, h = 1000, fonts = [1,2,4,8,16,32,64]
Output: 64
💡 Note: Single character 'a' can use the largest available font size on this large screen

Constraints

  • 1 ≤ text.length ≤ 50000
  • text contains only lowercase English letters
  • 1 ≤ w ≤ 107
  • 1 ≤ h ≤ 104
  • 1 ≤ fonts.length ≤ 105
  • 1 ≤ fonts[i] ≤ 105
  • fonts is sorted in ascending order and does not contain duplicates

Visualization

Tap to expand
Maximum Font to Fit a Sentence in a Screen INPUT Screen w=80, h=20 "helloworld" text = "helloworld" fonts[] (ascending) 6 8 10 12 14 16 18 24 36 Constraints: width = 80, height = 20 text length = 10 chars FontInfo Interface: getWidth(fontSize, char) getHeight(fontSize) ALGORITHM STEPS 1 Binary Search Setup lo=0, hi=8 (array indices) 2 Check Middle Font mid=4, font=14 3 Calculate Dimensions Sum widths, check height 4 Adjust Search Range If fits: try larger font Binary Search Trace: mid=4 (font=14) --> TOO BIG mid=1 (font=8) --> TOO BIG mid=0 (font=6) --> FITS! Result: font size 6 Fit Condition: totalWidth <= w AND fontHeight <= h FINAL RESULT Screen (80 x 20) "helloworld" Font Size: 6 Output: 6 Verification: Font 6: height <= 20 OK Total width <= 80 OK Maximum valid font = 6 Status: OK Text fits on screen! Key Insight: Binary search on sorted fonts array achieves O(log n * m) complexity where n = fonts count, m = text length. The monotonic property (if font X doesn't fit, no larger font will fit) enables binary search optimization. We search for the rightmost font that satisfies both width and height constraints. TutorialsPoint - Maximum Font to Fit a Sentence in a Screen | Optimal Solution (Binary Search)
Asked in
Google 12 Facebook 8 Amazon 5
28.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen