Problem Statement
A lattice crossword is a variant of a crossword where exactly 4 words are arranged in the form of a lattice (like a '#' sign). Given 4 words, a, b, c and d, return the number of different lattice crosswords that can be created according to the following rules:
- Each of the four given words must appear in the crossword exactly once.
- Two of the four words must be printed horizontally, from left to right.
- Two of the four words must be printed vertically, from top to bottom.
- Each horizontal word must cross both vertical words (each through exactly one common letter).
- Each vertical word must cross both horizontal words (each through exactly one letter).
- The horizontal words must be separated by at least one row.
- The vertical words must be separated by at least one column.
Two lattice crosswords are considered different if at least one of the words is at a different position. The position of a word is determined by the row and column position of its first letter. Row 0 is the row of the topmost letter in the crossword, and column 0 is the column of the leftmost letter in the crossword.
c
o
p n
r t
topcoder
b s
solution
e
m
Here the leftmost letter is "t" of the "topcoder" and the topmost letter is "c" of the "contest".
So, the positions of the words are:
"topcoder" - (0, 4)
"contest" - (6, 0)
"problem" - (4, 2)
"solution" - (2, 6)
Another one lattice crossword, which can be constructed using this words is:
t
o
p s
c o
problem
d u
contest
e i
o
n
Definition
- Class:
- LatticeCrossword
- Method:
- crossword
- Parameters:
- String, String, String, String
- Returns:
- int
- Method signature:
- int crossword(String a, String b, String c, String d)
- (be sure your method is public)
Constraints
- a, b, c and d will each contain between 3 and 15 characters, inclusive.
- a, b, c and d will be distinct.
- a, b, c and d will contain only lowercase letters ('a'-'z').
Examples
"topcoder"
"contest"
"problem"
"solution"
Returns: 2
Example from the problem statement."coder"
"number"
"rober"
"joker"
Returns: 0
No lattice crossword can be constructed."lattice"
"crossword"
"disticnt"
"approach"
Returns: 4
"zaxb"
"axc"
"cxd"
"bxdy"
Returns: 2
Two lattice crosswords: z zaxb axc x x and x x cxd bxdy y"aaaaaaaaaaaaaaa"
"aaaaaaaaaaaaba"
"aaaaaaaaaaaaaab"
"baaaaaaaaaaaaaa"
Returns: 10082176
A lot of crosswords.