Question
Add your code to the shell below. The desired method determines if one int array is a permutation of another int array. A permutation, also
Add your code to the shell below. The desired method determines if one int array is a permutation of another int array.
"A permutation, also called an "arrangement number" or "order " is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself." [mathworld.wolfram.com] For example the list (I, 2} has the following permutations; (I, 2} and {2, 1}
Note the elements of listA and listB are lists, not sets, so duplicate items could appear. So for example given the list (I, 2, 2} the unique permutations are (I, 2, 2), {2, 1, 2}, and (2, 2, 1}. (2, 1} is not a permutation of (1, 2, 2}., Another example of lists that are not permutations of each other: (2, I, 1} is not a permutation of {2, 2, 1}.
/* Determine if listA is a permutation of listB. pre: listA != null, !=null, listB != null post: return true if listB is a permutation of listA, false otherwise. Neither listA or listB are altered as a result of this method. */
public static boolean isPermutation(int[] listA, int[] listB)
Hint: Do not try to solve the problem by taking one of the arrays and generating all the permutations of that array and then check to see if the other array is one of those permutations. That is too inefficient except for arrays with a very small number of items.
1 package extraCredit;
2
3 public class Permutation [
4 /**
5 * Determine if one array of ints is a permutation of another
6 * Neither the parameter listA or
7 * the parameter listB are altered as a result of this method. 8 * @param listA !- null
9 * @param listB != null
10 * @returr: true if listB is a permutation of listA,
11 * false otherwise
12 *
13 */
14 public static boolean isPermutation(int{] listA, int[] listB) {
15 // check preconditions
16 if (listA == null l l listB == null)
17 throw new IllegalArgumentException( "Violat ion of precondition: +
18 isPermutation. neither parameter may equal null.");
19 /*CS2320 STUDENTS: INSERT YOUR CODE HERE*/
20
21 return true; // must change
22 }
23 public static void main(String[] args) {
24 // TODO Auto-generated method stub
25
26 }
27
28 }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started