Statistics

Problem Statement for "TemplateMatching"

Problem Statement

In this problem you will be given a String text and you will need to find the substring of the text that matches a given template in the best way. The template will be represented by two Strings: prefix and suffix. Consider a string S. The prefix match score of S with respect to a given template is the maximal n >= 0 such that the first n characters of S are equal to the last n characters of prefix and occur in the same exact order. Analogously, the suffix match score of S is the maximal m >= 0 such that the last m characters of S are equal to the first m characters of suffix and occur in the same exact order.


For example, if S = "something", prefix = "awesome", and suffix = "ingenious", than the prefix score of S is 4 (the matched characters are "some") and the suffix score is 3 (the matched characters are "ing").


The match score of a string S with respect to a given template is the sum of its prefix and suffix match scores. Find the non-empty substring of text with the maximal match score according to the template (prefix, suffix). In case of a tie, return the substring with the maximal prefix score. If there are still several candidates, return one that comes first lexicographically.

Definition

Class:
TemplateMatching
Method:
bestMatch
Parameters:
String, String, String
Returns:
String
Method signature:
String bestMatch(String text, String prefix, String suffix)
(be sure your method is public)

Notes

  • String A comes before string B lexicographically if A is a proper prefix of B, or if A has a smaller character at the first position where the strings differ. When comparing the characters, refer to the following list of characters in ascending order: ' ', 'a', 'b', ..., 'z'.

Constraints

  • text will contain between 1 and 50 characters, inclusive.
  • prefix will contain between 1 and 50 characters, inclusive.
  • suffix will contain between 1 and 50 characters, inclusive.
  • text, prefix and suffix will contain only lowercase letters ('a'-'z') and spaces (' ').

Examples

  1. "something"

    "awesome"

    "ingenious"

    Returns: "something"

    The example from the problem statement.

  2. "havka"

    "eto"

    "papstvo"

    Returns: "a"

    The return value must be non-empty string, so the correct answer is "a".

  3. "a"

    "b"

    "c"

    Returns: "a"

    The return value must be non empty string, so the correct answer is "a".

  4. "abracadabra"

    "habrahabr"

    "bracket"

    Returns: "abrac"

  5. "mississippi"

    "promise"

    "piccolo"

    Returns: "ippi"

  6. "a a a a a a"

    "a a"

    "a"

    Returns: "a a"

  7. "z vt no k bdmztq ct lk ot e bref vr czff"

    "tcrmy qrxw cl w"

    "cj zi dp"

    Returns: " bdmztq c"

  8. " e mcytuzce u msgh fgi lw quyukw b"

    " wapl yl s kq"

    "abw rv"

    Returns: "q"

  9. "ogpu rjgq mtf z"

    " k"

    "gzk"

    Returns: " rjg"

  10. "n xfixx g j ouii tez uaqmehobr bh ltdw qu"

    "z s mi nv xlc a"

    "qif"

    Returns: "aq"

  11. " yvy iz hoc t awib p g"

    " yl g"

    " n s"

    Returns: " g"

  12. "x jv u tnj jfdh hz zhr emo nmv"

    "gmu"

    "m c"

    Returns: "u tnj jfdh hz zhr em"

  13. " a pc e cg h y g opyeijc t eonjwoh kdm q o"

    "h"

    "m rhnp nnzig qj"

    Returns: "h kdm "

  14. "e pnwd zwthplk l k ybkbv elbbcqi w cbv d wyz wab"

    " ln irp qamkui g d ndwb"

    "pj hci ef t v r j ulpdg p i j"

    Returns: "b"

  15. " xx crahded mitc aj i y k qiamjtls"

    " ud kg"

    "acp fte iaj"

    Returns: " a"

  16. "eu mdvv un z y laer fk vk f vx x xb g kwmu gg"

    " e iua a"

    "t kpwbd m"

    Returns: "a"

  17. "gn i"

    "ro"

    " h"

    Returns: " "

  18. "bwpoq hd jo q recw bg obmq k vr ct kkljcjx"

    "u ljikxxhzc as"

    "hyd p"

    Returns: " h"

  19. " xb yl qmof di ctqpq na icdrlgkq fjbej"

    " n"

    " lw"

    Returns: " na "

  20. "kqkoefah ys i jfhguzrd qm q k lra"

    "qe kp mmr"

    "m dfp bc y m vt"

    Returns: "rd qm "

  21. "tmuh rlbfqxdn t g atd zggby"

    "gfv fb"

    " mo un vpi v s"

    Returns: "bfqxdn "

  22. "zppowg qzreqw rjj qh pixa v"

    "suc"

    "zpa d t"

    Returns: "zp"

  23. "eq hc q t lnvw"

    "x"

    " ef ok t"

    Returns: " "

  24. " u wczb v"

    "z"

    "sox"

    Returns: "z"

  25. "bqevduspk jn jnw k u gv yf ax bjwwis ymh w knup"

    "ghwdrovpwwbywm ryr"

    "pp qqd"

    Returns: " ax bjwwis ymh w knup"

  26. " fed vvd gweac odk a lbderviy la q tyar r snr p"

    "o yhg rgda"

    "zv opmmcquup"

    Returns: "a"

  27. "kgs gjb klormak s cbx xvh im oaj o dt c n l"

    "b j dojez foxyi"

    " yto v z aibbe r pde g"

    Returns: "im "

  28. " xcq n l ldro k kgb n j xwkno ukc b l t r agmah b"

    "p y he"

    " bwokq hav"

    Returns: " agmah b"

  29. " cu ldp oyjy c tso rw b hkp pd"

    " bm q ihn"

    " ccxw"

    Returns: " c"

  30. " kcy"

    " g"

    "o x"

    Returns: " "

  31. "gpz t c vd u yqof"

    "on fkz"

    " n"

    Returns: "z "

  32. " xnt reje l uvi lzca tlbqelkjd wp kik"

    "d zpg yvnota"

    "o"

    Returns: "a"

  33. "fl wn dkf"

    " vx m"

    "d"

    Returns: " d"

  34. "jhxc le lizh zt"

    "w"

    "vvucg v"

    Returns: " "

  35. "qn okxyo"

    " ybx p"

    " d"

    Returns: " "

  36. "pcu r byga x g z k ab x hcn e qw th"

    "ktukuq j q"

    "pp gmi"

    Returns: " q"

  37. " dkx vo j"

    "g"

    "sqc"

    Returns: " "

  38. "r o tejmrmdpdyu daz zw jnattfbhpo"

    "o gt p jcnapp"

    " ksd f ww y"

    Returns: "pdyu "

  39. " d wbhxydy v twjpc aj c dazklu qt cexf ed"

    " ycc i dgto vn"

    " lgf zz q g"

    Returns: " "

  40. "ig lv msttp wk iuki ezwfnuxdq odg"

    "wf lsi ua hbaa"

    "epg mfvs ft lpwk x"

    Returns: " e"

  41. " k t zdr"

    "m"

    "c"

    Returns: " "

  42. " ght w xq g m g r"

    " s w ue"

    "tp"

    Returns: " ght"

  43. "q kbf pzyc tqlw ateaozmdprzkw s t v ieyofyn"

    "dtmx w mehp e"

    "gkkac"

    Returns: "e"

  44. "z s agzjztn iqo b qnu m d zb jd hi figsckyho q"

    "f rfkw dxt hf q m"

    "jrxwfis xt f padqx c hfl ut "

    Returns: " m d zb j"

  45. "x gv xdw vvj v hmlwsaz atftosucne fc ocf"

    " pryxfkt v s m z wr k p jftz byc "

    "s i arf wyleqmyou frxs lk"

    Returns: "c "

  46. " p lqq g ix ndc oix gnh lk qiw gafz mzqxqp vxw "

    " qcsq mn"

    "u"

    Returns: "n"

  47. "e stjlv s iis lphme io k jgvj ls p at"

    "svfv a zic dlz sb gej "

    " w x tpye "

    Returns: "j "

  48. " teysti ycqvy djqvu zpusk py iflwnmf zf cw e"

    " l f m dbwr pr dtg"

    " s y vt tjb e q w l upovs"

    Returns: " "

  49. "bg f mcc pgviscr vd d orom qje bnduuoc"

    "qfof hyfkix xiyuxk p"

    " s cca onl u"

    Returns: " pgviscr "

  50. "opgez sii rahs ny em xwy crboz zfmnkkoioe "

    " lfwo bpww nd lfkaa ftvemdmdvc wom ie"

    " sxiu ws d r j obbu xnsm nfs ond sa"

    Returns: "ez s"

  51. "ml ctqwd soaug dhwj zm u ewwo z ef"

    "mdy tcxsvja s bsng zjqrpya ajsdy sl k c l "

    "qciwpmeti hn j w tr hhisdi bhzq h "

    Returns: "l ctq"

  52. "lcxeux pambpclh gs i ik eg n lkmn aw yl"

    "y dsg e uqtz th xvm wahe "

    " r tmp hb j wrzb odhgon jniq ov zir"

    Returns: " "

  53. "ayo lth y f zhosk qphy gryyt og dmpnzm g we "

    " j f y gx tezejhrx atizkmu x "

    " ou wftloc n hcnkbzv slhv"

    Returns: " f zhosk qphy gryyt o"

  54. "mwdvp bu lu z gbnex h zqd njfe denxm yplj"

    "frktdsdyqx gv w pey"

    "slpxkqltc t ffhlbfcywtf"

    Returns: "y"

  55. "cz cu orlmjfksa jlqabom dh kqbtzbs qavdo k v "

    "x hcb zho zn arg cuu udc mb"

    " npo e pedhtbh uvw "

    Returns: "bom "

  56. " we sybh nr pxrrq m hob pqcenf ntjzypxo"

    " e slkmbu mlpxsyai g efryp opl qw aa x"

    "zrie qujljv nglukgam enhjq"

    Returns: "xrrq m hob pqcenf ntjz"

  57. "f fv hr jakiy qz o zxaismar p ergzmoovxojwg"

    "xc g d ul r a o l ym urin lolg c csni "

    "nq eyeb"

    Returns: " "

  58. " jkqcquk tfdrkbtffr t fk bctrcbkbjcvc r sm y mm"

    " y mndmwvue t yptd f dij"

    "n y slowf mtgew g lewkb "

    Returns: "j"

  59. "qythmoqh rq boftg ypex g og g p wis c dh r"

    " frnjzkvuqv lw gi gy dftt isgmmwds aupdhi"

    "ay w f y pu dcq kdu nyn "

    Returns: "i"

  60. "qp pesgwq ox ue n up xeuopw w ypawctwax qs g"

    "z fym jh b uq rufmp heogc mwvxz zrnuci"

    "cjszu soubczr"

    Returns: " n up xeuopw w ypawc"

  61. "kay msqblixtzxau qit omk xz kltfmevbdsvj ud"

    "zsvl gpmk ionqrgjuirbss no xbos pfl"

    "w"

    Returns: "l"

  62. "syvf yh g brfsj vxhgofk k ecey zknp ttp cjn tg "

    "rn ofjic i dbtji a y szdqo"

    " "

    Returns: "ofk "

  63. "qlmujs midzkpuon glikqm t t puxu c jspmbpujur"

    "t zui ppw jyd "

    "uof w k slkfkf sfbhsu vbz vsa kznq "

    Returns: " midzkpuo"

  64. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    Returns: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

  65. " oriyty gln ghn zhpxyzqqn ar vxbln dg zmzzfcaf "

    " d ox rds gwblg bcx qb vf czf kdgyliyy j esoy"

    "bfbpi hhlxg obd g e ruqhq ie xy lxm t u ef "

    Returns: "y gln ghn zhpxyzqqn ar vxb"

  66. "ahdaqbfwyjc y x h qlkteom mf it krj juuvbsttva f"

    " h ptnybkqd m wemklzn o fcf breg ie lyxlx w"

    "b gs b t u af mw p zj jq iwdg ew t w h uh"

    Returns: "wyjc y x h qlkteom mf it krj juuvb"

  67. "wrr zzo qt hgm rpneh awte o pcjvaa rb axgu cx"

    "yigx e wpujledx ohut a ey v de lkzt x juvl"

    "h ke rjpskpidwbg zojhc rsdjsi hobbxglyr rp tgh a "

    Returns: " zzo qt hgm rpneh "

  68. "wrr zzo qt hgm rpneh awte o pcjvaa rb axgu cx"

    " zzo qt hgm rpneh awte o pcjvaa rb axgu cx"

    "wrr zzo qt hgm rpneh awte o pcjvaa rb axgu"

    Returns: " zzo qt hgm rpneh awte o pcjvaa rb axgu cx"

  69. "wrr zzo qt hgm rpneh awte o pcjvaa rb axgu cx"

    "wrr zzo qt hgm rpneh awte o pcjvaa rb axgu"

    " zzo qt hgm rpneh awte o pcjvaa rb axgu cx"

    Returns: "wrr zzo qt hgm rpneh awte o pcjvaa rb axgu cx"

  70. "ab"

    "b"

    "a"

    Returns: "b"

  71. "missidsgssippi"

    "proddmise"

    "piddccolo"

    Returns: "dsgssippi"

  72. "bcd"

    "efg"

    "hjk"

    Returns: "b"

  73. "b"

    "x"

    "y"

    Returns: "b"

  74. "abababbab"

    "ababababaab"

    "abaababab"

    Returns: "aba"

  75. "aaaa aaaaaa"

    " qqqqqqqqqq"

    " ssss s"

    Returns: " "

  76. "iiiiii"

    "a"

    "b"

    Returns: "i"

  77. "tttt"

    "aaa"

    "bbb"

    Returns: "t"

  78. "bbbb"

    "aaaa"

    "ccccc"

    Returns: "b"

  79. " "

    "a"

    "b"

    Returns: " "

  80. "r"

    "x"

    "b"

    Returns: "r"

  81. "bc"

    "d"

    "e"

    Returns: "b"

  82. "zz"

    "a"

    "a"

    Returns: "z"

  83. "b"

    "a"

    "a"

    Returns: "b"

  84. "bacabadabacabaeabacabadabacaba"

    "abacabada"

    "bacac"

    Returns: "abacabadabaca"

  85. "az"

    "b"

    "c"

    Returns: "a"

  86. "abc"

    "e"

    "f"

    Returns: "a"

  87. "abcdefghijklmnopq r s t uvwx"

    "abcdefg r s jldf fjd"

    "uvwxyz"

    Returns: "defghijklmnopq r s t uvwx"

  88. "bxxxxxefgcdxxxxabxxxxcd"

    "ttab"

    "efgcdtt"

    Returns: "bxxxxxefgcd"

  89. "cabacbaddadaaddbabccaddaddbdacadbabddbbcdacdcbc"

    "dbddaddaaadacadabdaadbbcaadddaaaabcbdabadaabcdcdb"

    "dbddaddaaadacadabdaadbbcaadddaaaabcbdabadaabcdcdb"

    Returns: "dbabccaddaddbd"

  90. "hzvkz"

    "eto"

    "papstvo"

    Returns: "h"

  91. "abcd"

    "abc"

    "bcd"

    Returns: "abcd"

  92. "abdbaabcba"

    "ab"

    "ba"

    Returns: "abcba"

  93. "aafdasdfasdaasfdasdffdasdfasdaafdasdfasd"

    "afasdfatrhertghsfsdhsdfaassaafdasdfasddfasdfasdf"

    "aasdfasdfaafdasdasfasdfasfdasfdyrefasdaafdasdfasda"

    Returns: "asdfasdaas"

  94. "aaaaa"

    "f"

    "aaaa"

    Returns: "aaaa"

  95. "z"

    "x"

    "y"

    Returns: "z"

  96. "b"

    "c"

    "d"

    Returns: "b"

  97. "bcd"

    "x"

    "z"

    Returns: "b"

  98. "xxxeabxxxfeaxxx"

    "dfe"

    "abc"

    Returns: "fea"

  99. "axxxxxxxtyyyaxxxxkkktyyy"

    "llla"

    "tlll"

    Returns: "axxxxkkkt"

  100. "ab"

    "fgg"

    "ghhh"

    Returns: "a"

  101. "aba"

    "xxx"

    "aba"

    Returns: "aba"

  102. "bb"

    "a"

    "c"

    Returns: "b"

  103. "bcd"

    "gf"

    "ij"

    Returns: "b"

  104. "chetan"

    "he"

    "ta"

    Returns: "heta"

  105. "havak"

    "eto"

    "papstvo"

    Returns: "a"

  106. "zbcdefghjik"

    "q"

    "zbcdefghjik"

    Returns: "zbcdefghjik"

  107. "ab"

    "x"

    "y"

    Returns: "a"

  108. "x"

    "a"

    "b"

    Returns: "x"

  109. "zz"

    "ab"

    "ba"

    Returns: "z"

  110. "abcdftab"

    "tab"

    "c"

    Returns: "tab"

  111. "dfd"

    "a"

    "a"

    Returns: "d"


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: