-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagrams.java
More file actions
32 lines (29 loc) · 898 Bytes
/
Anagrams.java
File metadata and controls
32 lines (29 loc) · 898 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
public class Anagrams {
public static void main(String[] args) {
String a = "abc";
String b = "cba";
boolean isAnagram = false;
boolean visited[] = new boolean[b.length()];
if (a.length() == b.length()) {
for (int i = 0; i < a.length(); i++) {
char c = a.charAt(i);
isAnagram = false;
for (int j = 0; j < b.length(); j++) {
if (b.charAt(j) == c && !visited[j]) {
visited[j] = true;
isAnagram = true;
break;
}
}
if (!isAnagram) {
break;
}
}
}
if (isAnagram) {
System.out.println("Anagram");
} else {
System.out.println("Not Anagram");
}
}
}