-
Notifications
You must be signed in to change notification settings - Fork 2
/
Anagram.java
37 lines (34 loc) · 846 Bytes
/
Anagram.java
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
import java.util.Scanner;
public class Anagram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first String");
String str1 = sc.next();
System.out.println("Enter the second String");
String str2 = sc.next();
boolean isAnagram= false;
boolean isVisited[] = new boolean[str2.length()];
if(str1.length()==str2.length()) {
for(int i=0;i<str1.length();i++) {
char c = str1.charAt(i);
isAnagram=false;
for(int j=0;j<str2.length();j++) {
if(str2.charAt(j)==c && !isVisited[j]) {
isVisited[j] = true;
isAnagram = true;
break;
}
}
if(!isAnagram) {
break;
}
}
}
if(isAnagram) {
System.out.println("Anagram");
}
else {
System.out.println("Not Anagram");
}
}
}