Statistics

Problem Statement for "FourStrings"

Problem Statement

We have four Strings: a, b, c, and d.

A superstring of our four strings is any string S such that each of the four strings occurs somewhere in S as a contiguous substring. Note that some superstrings of our four strings always exist.
For example, the string S = a+b+c+d is obviously a superstring of a, b, c, and d.

Find and return the length of the shortest superstring of a, b, c, and d.

Definition

Class:
FourStrings
Method:
shortestLength
Parameters:
String, String, String, String
Returns:
int
Method signature:
int shortestLength(String a, String b, String c, String d)
(be sure your method is public)

Constraints

  • a will contain between 1 and 10 characters, inclusive.
  • b will contain between 1 and 10 characters, inclusive.
  • c will contain between 1 and 10 characters, inclusive.
  • d will contain between 1 and 10 characters, inclusive.
  • Each character in a will be a lowercase English letter ('a'-'z').
  • Each character in b will be a lowercase English letter ('a'-'z').
  • Each character in c will be a lowercase English letter ('a'-'z').
  • Each character in d will be a lowercase English letter ('a'-'z').

Examples

  1. "abc"

    "ab"

    "bc"

    "b"

    Returns: 3

    The shortest superstring in this test case is the string "abc". Note that each of the other three strings occurs in "abc" as a contiguous substring.

  2. "a"

    "bc"

    "def"

    "ghij"

    Returns: 10

    In this case, one possible shortest superstring is "abcdefghij".

  3. "top"

    "coder"

    "opco"

    "pcode"

    Returns: 8

    S = "topcoder"

  4. "thereare"

    "arelots"

    "lotsof"

    "ofcases"

    Returns: 19

  5. "aba"

    "b"

    "b"

    "b"

    Returns: 3

  6. "x"

    "x"

    "x"

    "x"

    Returns: 1

  7. "aaabab"

    "ba"

    "bbabab"

    "bbbbab"

    Returns: 13

  8. "aaaaa"

    "a"

    "aaaaaaaaaa"

    "aaaa"

    Returns: 10

  9. "aaaaaaaaaa"

    "bbbbbbbbbb"

    "cccccccccc"

    "dddddddddd"

    Returns: 40

  10. "abbabadbd"

    "bsddsbdsd"

    "bas"

    "d"

    Returns: 21

  11. "aba"

    "b"

    "b"

    "b"

    Returns: 3

  12. "ba"

    "a"

    "bbb"

    "a"

    Returns: 4

  13. "bbb"

    "baaa"

    "bbb"

    "bbab"

    Returns: 8

  14. "a"

    "aa"

    "b"

    "aabb"

    Returns: 4

  15. "a"

    "bbba"

    "ba"

    "aa"

    Returns: 5

  16. "bbbbba"

    "babaaa"

    "aab"

    "abaabab"

    Returns: 15

  17. "ba"

    "babab"

    "abb"

    "abbb"

    Returns: 7

  18. "a"

    "ab"

    "bb"

    "bb"

    Returns: 3

  19. "baabbab"

    "ababbaaa"

    "bbb"

    "babbaaa"

    Returns: 15

  20. "abbbb"

    "abbbaaa"

    "bb"

    "abaaabb"

    Returns: 15

  21. "ba"

    "babb"

    "b"

    "bb"

    Returns: 4

  22. "b"

    "aa"

    "b"

    "a"

    Returns: 3

  23. "bbabbbb"

    "ba"

    "ababa"

    "aaab"

    Returns: 14

  24. "bbcaa"

    "acbc"

    "cccc"

    "aa"

    Returns: 11

  25. "bbca"

    "bccaacbcac"

    "bac"

    "aa"

    Returns: 17

  26. "acaa"

    "c"

    "babaa"

    "aab"

    Returns: 9

  27. "ac"

    "b"

    "bab"

    "acc"

    Returns: 6

  28. "cbaacca"

    "aaaabcb"

    "b"

    "bb"

    Returns: 14

  29. "c"

    "baa"

    "abbc"

    "b"

    Returns: 6

  30. "ba"

    "bacca"

    "babbc"

    "bcabbaaa"

    Returns: 16

  31. "acbc"

    "abbabcc"

    "bacac"

    "acbaa"

    Returns: 18

  32. "c"

    "c"

    "cb"

    "cc"

    Returns: 3

  33. "aaccb"

    "baa"

    "a"

    "abbabc"

    Returns: 12

  34. "vkd"

    "q"

    "zhowey"

    "yqkcvhp"

    Returns: 15

  35. "h"

    "e"

    "m"

    "wjri"

    Returns: 7

  36. "eduu"

    "i"

    "htba"

    "chn"

    Returns: 12

  37. "k"

    "e"

    "h"

    "l"

    Returns: 4

  38. "pae"

    "sbsg"

    "mqjho"

    "e"

    Returns: 12

  39. "hsf"

    "ojdx"

    "pcvhx"

    "th"

    Returns: 13

  40. "c"

    "uu"

    "i"

    "qp"

    Returns: 6

  41. "egnxvve"

    "v"

    "meqqn"

    "ixspgyhscc"

    Returns: 22

  42. "loz"

    "bzlk"

    "cjrxm"

    "oi"

    Returns: 14

  43. "akdh"

    "rpsb"

    "dd"

    "w"

    Returns: 11

  44. "ab"

    "j"

    "de"

    "bcd"

    Returns: 6

  45. "aaaaaaaaab"

    "aaaaaaaaac"

    "aaaaaaaaad"

    "aaaaaaaaae"

    Returns: 40

  46. "topcod"

    "coder"

    "codcod"

    "ret"

    Returns: 13

  47. "arelots"

    "thereare"

    "lotsof"

    "cases"

    Returns: 19

  48. "dfdsfhfsdg"

    "uiodsfhfnb"

    "dfbwefeqpe"

    "fhudsfhqwn"

    Returns: 40

  49. "kl"

    "ly"

    "arkly"

    "klya"

    Returns: 6

  50. "ab"

    "bd"

    "ba"

    "ac"

    Returns: 6

  51. "abc"

    "cba"

    "aad"

    "diefur"

    Returns: 12

  52. "coder"

    "pcode"

    "top"

    "opco"

    Returns: 8

  53. "abc"

    "ab"

    "ab"

    "ab"

    Returns: 3

  54. "d"

    "dxxxxxxxxx"

    "exxxxxxxxx"

    "x"

    Returns: 20

  55. "abcdefghi"

    "efb"

    "hiabcd"

    "hiabcz"

    Returns: 18

  56. "abc"

    "ghij"

    "ceg"

    "egh"

    Returns: 8

  57. "abab"

    "abc"

    "bcb"

    "bcd"

    Returns: 8

  58. "abcd"

    "def"

    "cddef"

    "ef"

    Returns: 7

  59. "abb"

    "bbc"

    "bbb"

    "bbb"

    Returns: 5

  60. "thereare"

    "lotso"

    "ofca"

    "cases"

    Returns: 19

  61. "ef"

    "e"

    "cd"

    "ab"

    Returns: 6

  62. "abc"

    "b"

    "d"

    "e"

    Returns: 5

  63. "thereare"

    "sothere"

    "ere"

    "reare"

    Returns: 10

  64. "abcd"

    "abcd"

    "cdabcd"

    "abcdab"

    Returns: 8

  65. "asgrgrrr"

    "sadrgdas"

    "ferrrred"

    "we"

    Returns: 24

  66. "ab"

    "bc"

    "abdef"

    "a"

    Returns: 7

  67. "ab"

    "cd"

    "bdc"

    "e"

    Returns: 6

  68. "abc"

    "bcdy"

    "dba"

    "xab"

    Returns: 9

  69. "abcd"

    "bcde"

    "cdbc"

    "qqqq"

    Returns: 12

  70. "ab"

    "c"

    "bf"

    "gh"

    Returns: 6

  71. "abcratabc"

    "rat"

    "rat"

    "rat"

    Returns: 9

  72. "abcde"

    "ac"

    "ad"

    "bd"

    Returns: 11

  73. "abc"

    "b"

    "xyz"

    "y"

    Returns: 6

  74. "abc"

    "ae"

    "abckae"

    "a"

    Returns: 6


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: