forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnagramOfStrings.java
More file actions
35 lines (27 loc) · 823 Bytes
/
AnagramOfStrings.java
File metadata and controls
35 lines (27 loc) · 823 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
# This is the code for finding anagram of the string in java
import java.util.Scanner;
public class AnagramOfStrings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string : ");
String a = sc.nextLine();
System.out.print("Enter second string : ");
String b = sc.nextLine();
boolean isAnagram = false;
boolean visited[] = new boolean[b.length()];
if (a.length()==b.length()) {
for (int i=0; i<a.length(); i++) {
isAnagram = false;
for (int j=0; j<b.length(); j++) {
if (a.charAt(i)==b.charAt(j) && !visited[j]) {
isAnagram = true;
visited [j] = true;
break;
}
}if (!isAnagram) break;
}
}
if (isAnagram) System.out.println("anagram");
else System.out.println("not anagram");
}
}