Statistics

Problem Statement for "StringFragmentation"

Problem Statement

You are given a String containing a list of words separated by single spaces, with no leading or trailing spaces. You have a panel on which you would like to write these words, in order, possibly over multiple lines. The words must go from left to right within each line, and the lines must go from top to bottom. Each line must contain only complete words, and each pair of adjacent words in a line must be separated by a single space. There must be no leading or trailing spaces.

You must write the words in a font size greater than 7. In a font of size N, the height of each letter is 2*N pixels, and the width of each character (letters and spaces) is N+2 pixels. There is no space between adjacent characters and adjacent lines.

You are given a String text containing the words you must write. The dimensions of the panel in pixels are given in the ints width and height. You are not allowed to rotate the panel. Return the largest integer font size strictly greater than 7 that you can use to write the words on the panel, or -1 if it is impossible.

Definition

Class:
StringFragmentation
Method:
largestFontSize
Parameters:
String, int, int
Returns:
int
Method signature:
int largestFontSize(String text, int width, int height)
(be sure your method is public)

Notes

  • If you start a new line between two words, then the space that separated them in the input doesn't get written to the panel.

Constraints

  • text will contain between 1 and 50 characters, inclusive.
  • Each character in text will be an uppercase letter ('A'-'Z') or a space (' ').
  • text must be a list of words separated by single spaces, with no leading or trailing spaces, where each word is one or more uppercase letters.
  • width and height will be between 1 and 10000, inclusive.

Examples

  1. "ONE TWO THREE FOUR FIVE"

    150

    40

    Returns: 9

    With a font size of 9, we can write "ONE TWO THREE" on the first line and "FOUR FIVE" on the second line. The width of the first line is 13 characters * (9+2) pixels = 143 pixels. The width of the second line is 9 characters * (9+2) pixels = 99 pixels. The total height is 2 lines * (2*9) pixels = 36 pixels. The total size is therefore 143 x 36 pixels, which fits inside the 150 x 40 pixel panel. If you used a font size of 10, it would be 156 x 40 pixels, which would not fit.

  2. "ONE TWO THREE FOUR FIVE"

    150

    60

    Returns: 10

    Now we can write it in a font size of 10 by separating the text into three lines: "ONE TWO", "THREE", "FOUR FIVE".

  3. "ONE TWO THREE FOUR FIVE"

    150

    10000

    Returns: 28

    If you write each word on a separate line, you can use a font size of 28. The widest line would be "THREE", which is 150 pixels wide.

  4. "ONE TWO THREE FOUR FIVE"

    10000

    10000

    Returns: 1250

  5. "ONE TWO THREE FOUR FIVE"

    50

    50

    Returns: -1

  6. "A"

    9

    14

    Returns: -1

    Note that font size must be strictly greater than 7.

  7. "A"

    10000

    10000

    Returns: 5000

    max answer

  8. "A BB CCC A BB A BB CCC A BB CCC A A CCC BB A BB A"

    200

    200

    Returns: 16

    many splits possible

  9. "A"

    10

    16

    Returns: 8

  10. "A A A A A A A A A A A A A A A A A A A A A A A A A"

    13

    500

    Returns: 10

  11. "Y YYY YYYYYYY YY YYYYYYY YY YYYYYYYYYYYYYYYYYYYY Y"

    678

    321

    Returns: 31

  12. "YYYYYYYYYYYY YYY YYYYYYYYYYYYYYYYYY YY Y Y Y Y YYY"

    321

    123

    Returns: 15

  13. "QWERTYUIOPQWERTYUIOPQWERT YUIOPQWRTYUIOPQWERTYUIOP"

    1000

    100

    Returns: 25

  14. "QWERTYUIOPQWERTYUIOPQWERT YUIOPQWRTYUIOPQWERTYUIOP"

    1000

    300

    Returns: 38

  15. "QWER YUIOPQWERTYUIOPQWERT YUI PQWRTY IOPQW RTYUIOP"

    100

    100

    Returns: -1

  16. "QWER YUIOPQWERTYUIOPQWERT YUI PQWRTY IOPQW RTYUIOP"

    1000

    300

    Returns: 39

  17. "A A A A A A A AB A A A A A A A A A A A A A A A A"

    100

    5000

    Returns: 48

  18. "A A A A A A A A A A A A A A A A A A A A A A A A A"

    1000

    1234

    Returns: 109

  19. "A A A A A A A AB A A A A ABA A A A CCCC A A A A"

    1000

    1234

    Returns: 102

  20. "A Q WFD"

    30

    40

    Returns: 8

  21. "A Q WFD"

    29

    40

    Returns: -1

  22. "A Q WFD"

    30

    39

    Returns: 8

  23. "ZCOBZCNFWLQI TVDW XHRCBLDV YL GBUSBMBOR TLHCSMPXO"

    9559

    8322

    Returns: 794

  24. "NKEUFDXOTOG GX EYANFE CUKEPZSHKLJU GGEKJ QZJE PE"

    2669

    1764

    Returns: 203

  25. "OGV IEXDZUZO R R SVO BR MWZP W J"

    2674

    8297

    Returns: 332

  26. "XIQFL UUVEOO QCUDH EFNJ AIMUCZFSKU DUB RIS T RE"

    1081

    9201

    Returns: 106

  27. "POF HFO QVLQFXWWKMFX YYGM CASZSGOVSODKJG CWMB XRM"

    7919

    6424

    Returns: 563

  28. "MYJFJNHHSS C YDTEAMDCJB RH NEGYIWXGCJ LGRSM A"

    8435

    7129

    Returns: 700

  29. "HNI R QGJFWKJSM PJ AEF ZAAUL RCH CCDYR VVRIVUY EG"

    419

    1160

    Returns: 44

  30. "LV RZKKLDACLL EOJO ONXRQYJZGINRNNZ A XXAEDRWUDXZR"

    2579

    9262

    Returns: 169

  31. "FCIHUBSJO LM QSG MC HELSOTFLBG FNPCUZSRUPC YNVZ"

    7605

    1312

    Returns: 279

  32. "UG IWNI XDFJPWPXFBLKPN EELFJMTKUQPZOM NLMB PMKTLPT"

    6654

    2260

    Returns: 300

  33. "DSYDSGVFPENEMWBO"

    1563

    7376

    Returns: 95

  34. "UQHCESKMKHSSMVNONW FXWHGBIBABVQ"

    1456

    8137

    Returns: 78

  35. "UJK LXJOQKDVF PVLHV RWTF UKXFFJPS W XLIJJHE"

    7350

    4244

    Returns: 530

  36. "OZBAFPFMOWGRGO UATDQL HYGGY"

    8246

    7812

    Returns: 587

  37. "STZFL YAHORKTK GY OU ER L B CSXQLGYG MVLY XIX P W"

    6761

    227

    Returns: 113

  38. "KAEQMARVX MKRNJCOK FKTQJ C GBIDVB RNSAQWXTB"

    4856

    7202

    Returns: 537

  39. "PEN UXFLGBDWJMHJGUZ EU DFEIMO YFRQMFOZISSCCRIF"

    6709

    381

    Returns: 143

  40. "XY HKT SDGZX AEYKEX"

    277

    5861

    Returns: 44

  41. "EVJTFRAMUQGBDNR"

    3647

    149

    Returns: 74

  42. "LYZVQNKCXEKJQZ UESWQ NZPYV S KHSOIC HCZ"

    1052

    6499

    Returns: 73

  43. "NASLXW LLXLEWTZ WBSUVSENEIQWHNE OJKXHORYESUGDH JC"

    5321

    7863

    Returns: 352

  44. "FGNRDO ZPY MDLQ WQIOD WFYGKIV UJ"

    529

    926

    Returns: 73

  45. "YHCEJCJOJ NHGUST JFNECOLOCMFHP XTGFP"

    753

    2373

    Returns: 55

  46. "RLDURENNLQK HHYIPQZEV YKTBVTPW TTGECAJYLQVU ZFSOA"

    7402

    9041

    Returns: 614

  47. "LJZEGBLCATMV FQR"

    372

    6656

    Returns: 29

  48. "TNDPBBZDR LPHLGDYWD FNZ"

    7239

    450

    Returns: 225

  49. "EGVFJZBDWK EV GDWUBTJVTOQ TH GTDRDOL JMFUBZZ NHJ Q"

    3525

    2051

    Returns: 256

  50. "QBLHMR ZFGYKZ UCWWPDYJVCTVF M"

    4921

    8447

    Returns: 376

  51. "MJTMR JIVHSYTN M QJVMOHFYESQXDVR GF GYMHLWDXLOZMC"

    4973

    641

    Returns: 153

  52. "TCEXAMEY VIXDAQVSSD"

    43

    1557

    Returns: -1

  53. "J SJHQ M WZEDALKJ VMZGRXOKCH O L U CNQYR"

    6115

    5095

    Returns: 553

  54. "FLCEON Y QVURDI QMS TF W PEPU UGEAJDGZSJ W QRM ZS"

    3720

    9883

    Returns: 370

  55. "KDVWYRIZU LB GW D FZ WMOHS FWY AVZJAOX"

    3280

    2768

    Returns: 326

  56. "QYWOHWHRGV ZCDVSFDV PVSNLC NNDAEFGEN GLNXHOMK"

    1729

    3399

    Returns: 170

  57. "QHQGXATPXW"

    2838

    2586

    Returns: 281

  58. "UF N EBAVSP TVUCZJMUT XZY NIEHUESBLH PXNPOYMJ"

    181

    2573

    Returns: 16

  59. "B UPS AN AHRJ S X PM E OYMGPG OUKJV PUIHIJDTT O"

    7350

    1113

    Returns: 278

  60. "XJQ XOORDUUJIDX MEDEWDMM DBRR YZUFWBEC FVLXETWAX"

    992

    8505

    Returns: 88

  61. "GVHRGU MD MP TPBD HVLZMIOVRIWKB BY VEBY"

    2872

    3281

    Returns: 218

  62. "UT N TDANA R KXV YB J YLEB RER RMKAWXRR PWMZH CAQC"

    8141

    590

    Returns: 160

  63. "I MUKIYFU TGG MCFJ QE"

    5779

    172

    Returns: 86

  64. "RNNZDZM QEJVYA VYSIOTH IPFJEGEECKPSRCP IWKVTHBWGEX"

    8939

    5722

    Returns: 593

  65. "QTQR ETSECC O M YV EEA AM INI D O LNAKXJ BNDS"

    466

    5640

    Returns: 75

  66. "JOKKK NMI VSV YEEM RVMJ C XHFUOQHWTZA DNOA UNJ ENP"

    1612

    4614

    Returns: 144

  67. "MVVNJNDOZRXKXIF"

    8006

    6440

    Returns: 531

  68. "OFXBUWPU CL IKXUMGY W BE X VPBFLMH ZYLJ CV KMHU"

    3446

    5864

    Returns: 418

  69. "YXPRHOL JUPDJYHTLFGEICL UMLABKS OWNWVHQRMEFUOG"

    9479

    8420

    Returns: 629

  70. "C RV DTIK HITKB F H M EBY L D DFATM KLFEW"

    7403

    3288

    Returns: 491

  71. "V WN VYLUIHTCX PIHR RUNN UMJNWJLDUMLI L V"

    6424

    1836

    Returns: 306

  72. "RVHXPFVZSJJMIFU"

    909

    3045

    Returns: 58

  73. "A"

    100

    100

    Returns: 50

  74. "I"

    30

    15

    Returns: -1

  75. "A B C"

    37

    21

    Returns: -1

  76. "A"

    9

    127

    Returns: -1

  77. "A B"

    10

    31

    Returns: -1

  78. "A"

    9

    500

    Returns: -1

  79. "AAAAA AAAA A"

    71

    40

    Returns: 9

  80. "CHALLENGED"

    10000

    15

    Returns: -1

  81. "A"

    10

    8

    Returns: -1

  82. "ABC ABCD"

    40

    1000

    Returns: 8

  83. "AA"

    1000

    14

    Returns: -1

  84. "A A A A A A A A A A A A A A A A"

    10

    320

    Returns: 8

  85. "A A A A A A A A A A A A A A A A A A A A A A A A A"

    10

    10000

    Returns: 8

  86. "A"

    10000

    14

    Returns: -1

  87. "A"

    3002

    6000

    Returns: 3000

  88. "ONE"

    33

    22

    Returns: 9

  89. "A A"

    34

    20

    Returns: 9

  90. "A B"

    27

    16

    Returns: -1

  91. "A"

    10

    17

    Returns: 8

  92. "A"

    6

    10

    Returns: -1

  93. "ABRAKADABRAALAKAZAM"

    10000

    20

    Returns: 10

  94. "OTWO"

    150

    40

    Returns: 20

  95. "AAAA AA AA AA AA"

    80

    32

    Returns: 8


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: