-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathAnagram.java
50 lines (46 loc) · 1.32 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.Scanner;
/*This java program is used to find whether the strings are is anagram(An anagram is a word or phrase
* formed by rearranging the letters of a different word or phrase, typically
* using all the original letters exactly once) of each other or not or not.*/
public class Anagram {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
String b=sc.nextLine();
if(a.length()!=b.length())
{
System.out.println("Not Anagram");
}
else
{
boolean flag=true;
int arr[]=new int[256];
/*Storing Number of characters of string a in array according to ASCII values*/
for(char e: a.toCharArray())
{
arr[(int)e]++;
}
/*Storing Number of characters of string b in array according to ASCII values*/
for(char e: b.toCharArray())
{
arr[(int)e]--;
}
/*Comparing the arrays to find whether the characters are same or not in both strings*/
for(int i=0;i<256;i++)
{
if(arr[i]!=0)
{
flag=false;
}
}
if(!flag)
{
System.out.println("Not Anagram");
}
else
System.out.println("Anagram");
}
sc.close();
}
}