In: Computer Science
Create a static method with the appropriate inputs and outputs. Call each of them in the main method.
Write a method called isPermutaion() which takes in two List objects which contain the same types. It returns true if the Lists are permutations of each other and false if not. Two lists are permutations if they contain the same elements, including the same number of duplicates, but they don’t have to contain the elements in the same order. For example, [1,2, 4] and [2,1,4] are permutations of each other
Please Do this in Java.
//-------------- Permutation.java -----------
import java.util.*;
class Permutation
{
//static method that prints the list and it's
name.
public static void printList(List<Integer>
list,String listName)
{
System.out.println("\n-------------
"+listName +" -----------------\n");
System.out.println(list);
}
//function that returns a boolean value whether the
two lists are permutation to each other
public static boolean
isPermutation(List<Integer> one,List<Integer>
two)
{
//isPermutation checking without
changing the contents of two lists passed.
//copy elements of list one to
tempOne list
List<Integer> tempOne = new
ArrayList<Integer>(one);
//copy elements of list two to
tempTwo list
List<Integer> tempTwo = new
ArrayList<Integer>(two);
//Sort the two lists
Collections.sort(tempOne);
Collections.sort(tempTwo);
//now compare the two lists.
return
tempOne.equals(tempTwo);
//if you don't want to copy the
contents of the two lists passed into temp lists
//please put comments to all the
above statements and uncomment below 3 statements
//isPermutation checking by sorting
the contents of two lists passed and comparing.
//Collections.sort(one);
//Collections.sort(two);
//return one.equals(two);
}
//main method to test
public static void main(String[] args)
{
//Test - 1
//create two lists
List<Integer> one = new
ArrayList<Integer>();
one.add(1);
one.add(2);
one.add(4);
List<Integer> two = new
ArrayList<Integer>();
two.add(2);
two.add(1);
two.add(4);
//print them
printList(one,"List One");
printList(two,"List Two");
//call the isPermutation() method
on the two lists.
System.out.println("\nIs list One
is Permutation of list Two: "+isPermutation(one,two));
}
}
//
//if you have any doubts please comment and like the answer
please.