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.
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
"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."a"
"bc"
"def"
"ghij"
Returns: 10
In this case, one possible shortest superstring is "abcdefghij"."top"
"coder"
"opco"
"pcode"
Returns: 8
S = "topcoder""thereare"
"arelots"
"lotsof"
"ofcases"
Returns: 19
"aba"
"b"
"b"
"b"
Returns: 3
"x"
"x"
"x"
"x"
Returns: 1