Statistics

Problem Statement for "ClosestRegex"

Problem Statement

A regular expression is a pattern describing how a particular string should be formed. For the purposes of this problem, a regular expression consists of atoms. Each atom is either a single lowercase letter (which matches exactly one of that letter), or a single lowercase letter followed by an asterisk ('*'), which matches zero or more of that letter. A string matches a regular expression if it can be partitioned into substrings which match the atoms of the regular expression in the same order. For example, the regular expression ab*c*b is matched by the strings ab, abb, acb and abbbcccb, but not by ba, accbb or babcb.

You have a string text which ought to match the regular expression regex. However, it may have been corrupted. Return the string S that satisfies the following conditions.

  1. S has the same number of characters as text.
  2. S matches regex.
  3. The number of positions in which S differs from text is minimal.
  4. S is lexicographically smallest amongst strings that satisfy the other conditions.

If there is no string that satisfies the above conditions, return the empty string.

Definition

Class:
ClosestRegex
Method:
closestString
Parameters:
String, String
Returns:
String
Method signature:
String closestString(String text, String regex)
(be sure your method is public)

Constraints

  • text will contain between 1 and 50 characters, inclusive.
  • text will contain only lowercase letters ('a' - 'z').
  • regex will contain between 1 and 50 characters, inclusive.
  • regex will contain only lowercase letters and '*'s.
  • The first character of regex will not be a '*'.
  • regex will not contain two consecutive '*'s.

Examples

  1. "abcd"

    "bcdd"

    Returns: "bcdd"

    Here there are no asterisks, so only one string can match. 'a' must be changed to 'b', 'b' to 'c' and 'c' to 'd'.

  2. "topcoder"

    "t*px*coa*de*"

    Returns: "ttpcodee"

    Changing the string to ttpcodee requires two changes.

  3. "cmu"

    "c*m*fm*u*"

    Returns: "cfu"

    Any of fmu, cfu and cmf would require one change, but cfu is first lexicographically.

  4. "aaaaacccc"

    "a*abc*"

    Returns: "aaaaabccc"

  5. "short"

    "lo*ts*of*let*ter*s"

    Returns: ""

    No 5-letter string matches the regex.

  6. "abc"

    "def"

    Returns: "def"

  7. "abc"

    "ab"

    Returns: ""

  8. "abc"

    "ab*cd"

    Returns: "acd"

  9. "cbc"

    "a*b*"

    Returns: "abb"

  10. "acde"

    "a*bcde*"

    Returns: "bcde"

  11. "z"

    "a*"

    Returns: "a"

  12. "a"

    "z*"

    Returns: "z"

  13. "p"

    "p*"

    Returns: "p"

  14. "x"

    "x"

    Returns: "x"

  15. "x"

    "y"

    Returns: "y"

  16. "ababababababababababababababababababababababababab"

    "a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*"

    Returns: "aaaaaaaaaaaaaaaaaaaaaaaaaaabababababababababababab"

  17. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    "a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*"

    Returns: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

  18. "ababababababababababababababababababababababababab"

    "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*"

    Returns: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

  19. "bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxa"

    "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"

    Returns: "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"

  20. "cb"

    "c*ca*"

    Returns: "ca"

  21. "qq"

    "o*ot*a*a*p*a*n*"

    Returns: "oa"

  22. "bbbcaabcbc"

    "c*cba*a"

    Returns: "cbaaaaaaaa"

  23. "cbdaaeed"

    "e*e*ec*ee*"

    Returns: "ecccceee"

  24. "deeia"

    "g*gf*ha*f*"

    Returns: "gffha"

  25. "bdadddca"

    "b*bc*a*d*adc*c"

    Returns: "baadadcc"

  26. "ggooglegooglegooglegoogleoogle"

    "o*df*l*o*df*l*p*o*df*l*o*df*l*podfl*p*p*p"

    Returns: "oooodllloodllloodllloodlpodflp"

  27. "adadfbbasdfasdbfadfasdfqadfadfbadsfbadfadfd"

    "a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*r*s*t*u*v*"

    Returns: "aaaaabbbbdddddddddddddddddddddddddddddddddd"

  28. "shortestotnakraj"

    "lo*ts*of*let*ter*s*ealsr*asra*a*"

    Returns: "ltoleteealsasraa"

  29. "abfjdsjdhfsjfksdkjfywekjdssgkjkjjksjk"

    "j*h*f*z*d*g*d*d*d*s*f*fsg*f*fg*s*s*d*a*z*a*h*z*t*e"

    Returns: "jjjjjjjhhfsfffsffffffffffssssssssssae"


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: