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 Size to Fit Screen ProblemInputtext = "hello"w = 100, h = 50fonts = [6,8,10,12]ProcessTry font sizes:12→too big, 10→too big8→fits! Maximum foundOutput8Screen Constraint CheckWidth: text_width ≤ w AND Height: font_height ≤ h🎯 Find maximum font size that satisfies both constraints
Understanding the Visualization
1
Input
Text string, screen width/height, available font sizes
2
Process
Test font sizes to find maximum that fits
3
Output
Return largest fitting font size or -1
Key Takeaway
🎯 Key Insight: Use binary search on sorted font sizes to efficiently find the maximum size that fits both width and height constraints
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