Created
January 18, 2019 11:33
-
-
Save vignarajj/10372611169e27cbd0dbea1ce3fac786 to your computer and use it in GitHub Desktop.
Write a function that takes two arrays as input, each array contains a list of A-Z;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.HashSet; | |
public class SubsetExample { | |
public static boolean isSubset_simple(String arr1[],String arr2[]) { | |
int i = 0; | |
int j = 0; | |
int m = arr1.length; | |
int n = arr2.length; | |
for (i = 0; i < n; i++) { | |
for (j = 0; j < m; j++) { | |
if (arr2[i].equals(arr1[j])) { | |
break; | |
} | |
} | |
if (j == m) { | |
System.out.println("Value of j "+j); | |
System.out.println("Value of m "+m); | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean isSubset_hash(String arr1[], String arr2[]){ | |
int m = arr1.length; | |
int n = arr2.length; | |
HashSet<String> hset= new HashSet<>(); | |
for(int i = 0; i < m; i++){ | |
if(!hset.contains(arr1[i])) | |
hset.add(arr1[i]); | |
} | |
for(int i = 0; i < n; i++) { | |
if(!hset.contains(arr2[i])) | |
return false; | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
String arr1[] = {"A", "B", "C", "D", "E"}; | |
String arr2[] = {"A", "D", "Z"}; | |
if (isSubset_simple(arr1, arr2)) { | |
System.out.println("True"); | |
} else { | |
System.out.println("False"); | |
} | |
} | |
} | |
/* | |
* I'm using simple method [O(m*n)] and hashing method for results. | |
* | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment