- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagram.java
More file actions
Latest commit
50 lines (38 loc) · 1.25 KB
/
Copy pathAnagram.java
File metadata and controls
50 lines (38 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
packagecom.sarvesh.javabasics;
importjava.util.Arrays;
importjava.util.Scanner;
publicclassAnagram {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
System.out.print("Enter number of elements in 1st array: ");
intn = sc.nextInt();
sc.nextLine();
String[] arr1 = newString[n];
System.out.println("Enter elements of 1st array:");
for (inti = 0; i < n; i++) {
arr1[i] = sc.nextLine();
}
System.out.print("Enter number of elements in 2nd array: ");
intm = sc.nextInt();
sc.nextLine();
String[] arr2 = newString[m];
System.out.println("Enter elements of 2nd array:");
for (inti = 0; i < m; i++) {
arr2[i] = sc.nextLine();
}
if (isAnagram(arr1, arr2)) {
System.out.println("It's a valid anagram");
} else {
System.out.println("It's not a valid anagram");
}
sc.close();
}
privatestaticbooleanisAnagram(String[] arr1, String[] arr2) {
if (arr1.length != arr2.length) {
returnfalse;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
returnArrays.equals(arr1, arr2);
}
}