-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid anagram
More file actions
36 lines (26 loc) · 801 Bytes
/
valid anagram
File metadata and controls
36 lines (26 loc) · 801 Bytes
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
//valid anagram
// silent - listen
import java.util.Arrays;
class subj {
static boolean areAnagram(char[] str1, char[] str2) {
int n1 = str1.length;
int n2 = str2.length;
if (n1 != n2)
return false;
Arrays.sort(str1);
Arrays.sort(str2);
for (int i = 0; i < n1; i++) {
if (str1[i] != str2[i])
return false;
}
return true;
}
public static void main(String[] args) {
char str1[] = { 't', 'e', 's', 't' };
char str2[] = { 't', 't', 'e', 'w' };
if (areAnagram(str1, str2))
System.out.println("The two strings are anagram of each other.");
else
System.out.println("The two strings are NOT anagram of each other.");
}
}