Problem Statement
You are part of the development team for a fantasy role-playing game with many different creatures who speak many different languages. To simplify the language mechanism, your team decides to make every language a simple cipher of the English language. That means, there is a simple mapping between letters in the fake language and letters in English. If B maps to D and E maps to A, then "BEB" in the cipher language means "DAD" in English.
The team quickly finds out that not every random set of mappings will actually work.
1) First of all, self-mappings are not preferable, so you may not have a mapping of say G to G.
2) Next, although not every letter in English needs to be used, the set of letters in the cipher language and the set of letters used in the English language must be equinumerous. That means, there is a one to one mapping from the cipher language to English as well as a one to one mapping from English to the cipher language.
3) Lastly, because the role-playing game will use actual human speech, the team wants to ensure that vowels in the cipher language map to vowels in English (otherwise it would be too hard for the voice actors to speak cipher). Non-vowels must also map to non-vowels.
Write a method violation that takes as input a
Definition
- Class:
- GameCipher
- Method:
- violation
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int violation(String[] mappings)
- (be sure your method is public)
Notes
- Vowels are always A,E,I,O,U,Y. Note that Y is always a vowel for this problem.
- If there are multiple violations, return the index of the first one that causes a mapping violation.
- Indices are zero-based. The first element has index 0.
- If mappings contains a repeat mapping, then that is a violation, e.g. { "L-P", "A-E", "L-P" }.
Constraints
- Each element of mappings will be formatted exactly as an uppercase letter ('A'-'Z'), a '-', and another uppercase letter.
- There will be between 0 and 26 elements in mappings, inclusive.
Examples
{"A-E", "B-C", "C-D", "D-B"}
Returns: -1
Nothing is wrong with this cipher.
{"C-D", "Z-Z", "Y-Y"}
Returns: 1
There are two violations because of self-mappings. Return the earliest one.
{"B-D", "C-F", "L-M", "P-Y", "I-O", "A-Q"}
Returns: 3
There are again two violations because of a vowel mismatch. Return the earlier one (P-Y).
{"B-C", "X-Z", "B-D"}
Returns: 2
The violation is from the mapping of B-D, since an earlier mapping maps B-C. Return the mapping that actually causes the violation, which is B-D.
{"B-C", "D-C"}
Returns: 1
Again, the violation is that C is already used, so return the mapping that causes the violation.
{"A-E", "E-A"}
Returns: -1
This is perfectly ok, even though the cipher is a reverse of itself.
{"A-E","B-C","C-D","M-N","G-H","H-Q","P-R","I-O","Y-I","K-J","J-K","S-X","V-T","W-V","X-Z"}
Returns: -1
{"A-E","B-C","C-D","M-N","G-H","H-Q","P-R","I-O","Y-Y","K-J","J-K","S-X","V-T","W-V","X-Z"}
Returns: 8
{"A-E","B-C","C-D","M-N","G-H","H-Q","G-J","I-O","Y-I","K-J","J-K","S-X","V-T","W-V","X-Z"}
Returns: 6
{"A-E","B-C","C-D","M-N","G-H","H-Q","P-R","I-O","Y-I","K-J","J-K","S-X","V-T","W-V","X-Z","A-E"}
Returns: 15
{"A-E","B-D","A-E"}
Returns: 2
You may not have the same mapping twice.
{"A-E","B-C","C-D","D-B"}
Returns: -1
{"C-D","Z-Z","Y-Y"}
Returns: 1
{"B-D","C-F","L-M","P-Y","I-O","A-Q"}
Returns: 3
{"B-C","X-Z","B-D"}
Returns: 2
{"B-C","D-C"}
Returns: 1
{"A-E","E-A"}
Returns: -1
{"A-E","B-C","C-D","M-N","G-H","H-Q","P-R","I-O","Y-I","K-J","J-K","S-X","V-T","W-V","X-Z"}
Returns: -1
{"A-E","B-C","C-D","M-N","G-H","H-Q","P-R","I-O","Y-Y","K-J","J-K","S-X","V-T","W-V","X-Z"}
Returns: 8
{"A-E","B-C","C-D","M-N","G-H","H-Q","G-J","I-O","Y-I","K-J","J-K","S-X","V-T","W-V","X-Z"}
Returns: 6
{"A-E","B-C","C-D","M-N","G-H","H-Q","P-R","I-O","Y-I","K-J","J-K","S-X","V-T","W-V","X-Z","A-E"}
Returns: 15
{"A-E","B-D","A-E"}
Returns: 2
{"B-C","D-C"}
Returns: 1
{"B-C","D-F","F-C","X-C"}
Returns: 2
{"A-E","S-T","T-V","E-Q"}
Returns: 3
{"A-B"}
Returns: 0
{"A-E","B-E"}
Returns: 1
{"C-D","B-D"}
Returns: 1
{"A-P","B-C","B-D"}
Returns: 0
{"C-B","D-B"}
Returns: 1
{"Z-A","Y-E","B-O"}
Returns: 0
{"A-Y","B-C","D-C"}
Returns: 2
{"B-C","D-C","B-E"}
Returns: 1
{"B-C","D-C"}
Returns: 1
{"B-C","D-C"}
Returns: 1
{"B-C","X-Z","B-D"}
Returns: 2
{"A-E","E-A","B-C","C-B","D-F","F-D","H-H"}
Returns: 6
{"A-B","B-A","B-C","D-C","C-D","G-G"}
Returns: 0
{"B-G","B-F"}
Returns: 1
{"A-Y"}
Returns: -1
{"B-Y","O-T"}
Returns: 0
{"B-C","D-C"}
Returns: 1
{"X-Z","A-I","X-T"}
Returns: 2
{"X-A"}
Returns: 0
{"B-C","D-C"}
Returns: 1
{"C-C"}
Returns: 0
{"B-C","B-D"}
Returns: 1
{"A-A"}
Returns: 0
{"G-A"}
Returns: 0
{"B-C","X-T","M-N","R-C"}
Returns: 3
{"B-C","X-Z","B-D"}
Returns: 2
{"X-A"}
Returns: 0
{"B-C","B-D","E-A"}
Returns: 1
{"A-E","T-P","P-X","A-A"}
Returns: 3
{"B-C","A-E","B-D"}
Returns: 2
{"A-Y"}
Returns: -1
{"C-E"}
Returns: 0
{"Z-B","Z-C"}
Returns: 1
{"Y-E","A-A"}
Returns: 1
{"U-O"}
Returns: -1
{"B-C","D-C"}
Returns: 1
{"Y-F"}
Returns: 0
{"B-C","X-Z","B-D"}
Returns: 2
{"A-E","E-A"}
Returns: -1
{"B-D"}
Returns: -1
{"A-A"}
Returns: 0
{"B-F","C-F"}
Returns: 1
{"A-E","B-C","I-E"}
Returns: 2